How to add line number using UNIX command?

Hi,

I am working on Shell script and I have a .dat file in which I want to add line numbers. Please see below example file

Input File:

19523479811841494432C2013052700000000
19523479811730333980A2013052700000000
19523479811417044397I2013052700000000
19523479811205895810A2013052700000000
19523479811689774317A2013052700000000
19523479811659473312A2013052720131231
19523479811841494432C2013052700000000
19523479811730333980A2013052700000000
19523479811417044397I2013052700000000
19523479811205895810A2013052700000000

Output File: (First six digit should be line number)

00000119523479811841494432C2013052700000000
00000219523479811730333980A2013052700000000
00000319523479811417044397I2013052700000000
00000419523479811205895810A2013052700000000
00000519523479811689774317A2013052700000000
00000619523479811659473312A2013052720131231
00000719523479811841494432C2013052700000000
00000819523479811730333980A2013052700000000
00000919523479811417044397I2013052700000000
00001019523479811205895810A2013052700000000
......
......
......
00009919523479811205895810A2013052700000000
00010019523479811205895810A2013052700000000

Thanks!

Please use code tags as required by forum rules!

$ cat -n file | tr ' ' '0' | tr -d '\t'
00000119523479811841494432C2013052700000000
00000219523479811730333980A2013052700000000
00000319523479811417044397I2013052700000000
00000419523479811205895810A2013052700000000
00000519523479811689774317A2013052700000000
00000619523479811659473312A2013052720131231
00000719523479811841494432C2013052700000000
00000819523479811730333980A2013052700000000
00000919523479811417044397I2013052700000000
00001019523479811205895810A2013052700000000

You could also try:

nl -ba -s "" -nrz file.dat

if you want something other than 6 digit line numbers, you can also add a -w option with an option argument specifying the number of digits you want.

Thanks Rudic. This was the first time I post query on this forum. Could you please tell me how I can use code tags and what I need to do for that?

Thanks!

Just read the "Quick Reply" hints, or, the forum rules.

 
When I do sort on original file it works but after adding line number it do not.
 
Original File:
19523479811841494432A2013052700000000
19523479811730333980A2013052700000000
19523479811417044397A2013052700000000
19523479811205895810A2013052700000000
19523479811689774317A2013052700000000
19523479811659473312A2013052720131231

sort -n -k11,21 filename.dat
 
19523479811205895810A2013052700000000
19523479811417044397A2013052700000000
19523479811659473312A2013052720131231
19523479811689774317A2013052700000000
19523479811730333980A2013052700000000
19523479811841494432A2013052700000000

File with line number
 
00000119523479811841494432A2013052700000000
00000219523479811730333980A2013052700000000
00000319523479811417044397A2013052700000000
00000419523479811205895810A2013052700000000
00000519523479811689774317A2013052700000000
00000619523479811659473312A2013052720131231

sort -n -k17,27 filename1.dat
 
00000119523479811841494432A2013052700000000
00000219523479811730333980A2013052700000000
00000319523479811417044397A2013052700000000
00000419523479811205895810A2013052700000000
00000519523479811689774317A2013052700000000
00000619523479811659473312A2013052720131231

 
Thanks!

You sorted using key from field 17 to field 27, which do not exist. Use field 1, char 17 - char 27 instead: sort -k1.17,1.27 .
BTW, no need to use numeric sort in this case!

Awesome. It worked. Thanks alot.