Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Custom Line Number/Prefix for text file using awk/sed

Before raising the flag I tried couple of things but didn’t get the desired result.

I have many text files which I want prefixed with custom numbering;

   the first 2 lines should be prefixed  00A:  , 00B:  
   and remaining lines should be incremental, like 001:  ,002:  ,003:  , and so on

Presently, I use this command for incremental numbering

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

awk '{printf("%03d:  %s\r\n", NR,$0)}' file1.txt > file2.txt

*which does the incremental nos. ok for the whole file; but not the multiple types needed.

Example Input file:

136725A6449C5279 933FB466C9CD699B
8FFBBA87E9D3209A AB41FBDC5E281A92
FF80DA7B0054FB29 006BF1C82C75C341
FA118264221B02A7 81E9A1FEB75FFB3D
31AA9FC566C3ADE0 70DDFD6DED2BF29C
F0B39014DA7FA6B1 77401108A81E33E1
74EF54060BC2B72F B5518D896DDC266F
DE10C97F9FBDA5A6 6C79566CA1BDC06E

Desired Output:

00A:  136725A6449C5279 933FB466C9CD699B
00B:  8FFBBA87E9D3209A AB41FBDC5E281A92
001:  FA118264221B02A7 81E9A1FEB75FFB3D
002:  31AA9FC566C3ADE0 70DDFD6DED2BF29C
003:  F0B39014DA7FA6B1 77401108A81E33E1
004:  74EF54060BC2B72F B5518D896DDC266F
005:  DE10C97F9FBDA5A6 6C79566CA1BDC06E

>Solution :

Neither Awk nor sed does this very well, but Perl has it built in.

perl -pe 'BEGIN { $prefix = "A"; }
  $prefix = "1" if ($. == 3);
  printf "%03s:  ", $prefix++;' file

The crucial feature here is that in Perl, "A"++ produces "B", natively. It doesn’t work so well with leading zeros, though; so I resorted to padding here.

Your question is rather unclear as to what should happen after 00Z or after 009 so I had to guess. For what it’s worth, in Perl, "Z"++ is "AA".

If you really insist on an Awk solution, it can be done with something like this:

awk '{ printf("%03s:  %s\n", (NR == 1 ? "A" : \
    (NR == 2 ? "B" : NR-2)), $0)}' file

I took out the fugly \r; if you are on Windows, maybe put it back (or consider your options).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading