How to add line number using UNIX command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add line number using UNIX command?
# 1  
Old 05-17-2013
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:

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

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

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

Thanks!

Last edited by Scrutinizer; 05-18-2013 at 03:47 AM.. Reason: code tags
# 2  
Old 05-17-2013
Please use code tags as required by forum rules!

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

# 3  
Old 05-17-2013
You could also try:
Code:
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.
# 4  
Old 05-17-2013
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!
# 5  
Old 05-17-2013
Just read the "Quick Reply" hints, or, the forum rules.
# 6  
Old 05-17-2013
Code:
 
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!

# 7  
Old 05-17-2013
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add line to previous line if not a number?

Hi, I am trying to compare 2 lists. However, one of these lists has to be taken from a.pdf file. When I copy the test into a .txt document there are formatting errors which I need to correct. The document is long (~10,000 lines) so I need to script the re-formatting. Currently my file looks... (9 Replies)
Discussion started by: carlr
9 Replies

2. Shell Programming and Scripting

Add line number to for loop?

> cat test.sh for t in `cat out.txt` do echo create directory data as "'"$t"';" done > output of out.txt is as below /oracle/SID/data1/file2_1/ /oracle/SID/data1/file1_1/ /oracle/SID/data1/file5_5/ /oracle/SID/data1/file_9/ /oracle/SID/data1/file_2/ when i run my test.sh... (6 Replies)
Discussion started by: crazy_max
6 Replies

3. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

Choose a line-number to add line

Hello guys, I'm making a script to add visudo with this script. Do you guys know if it's possible to add words to a line-number you want to. Something like this: echo "Adding words to line-number 16" >> /etc/sudoers # (options to add to line-number-16)? Thanks! (3 Replies)
Discussion started by: dannyvdberg
3 Replies

5. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

6. Shell Programming and Scripting

add a number to the beginning of every line

hey, i would like to add a line number to the beginning like so: red blue green yellow will be: 1=>red 2=>blue 3=>green 4=>yellowplease advise thank u. (5 Replies)
Discussion started by: boaz733
5 Replies

7. Shell Programming and Scripting

add number in lines line by line in different files

I have a set of log files that are in the following format ======= set_1 ======== counter : 315 counter2: 204597 counter3: 290582 ======= set_2 ======== counter : 315 counter2: 204597 counter3: 290582 ======= set_3 ======== counter : 315 counter2: 204597 counter3: 290582 Is... (6 Replies)
Discussion started by: grandguest
6 Replies

8. Shell Programming and Scripting

To add a number at the end of the line

Hi Folks, Using the Vi, how can I add a numbers at the end of the line. For eg: I have the numbers in the file as: 58.125.33 22.58.68 25.144.225 114.25.38 I need to add .0/8 at the end of all the line. So, it should be like 58.125.33.0/8 22.58.68.0/8 25.144.225.0/8 114.25.38.0/8 (6 Replies)
Discussion started by: gsiva
6 Replies

9. Shell Programming and Scripting

making sure a command line paramter is a number

i need to make sure that a command line paramter is with in a certin set of numbers and i dont know how todo it with out checking individual numbers. if test $1 -eq (need something here) then echo hi fi like if i put individual numbers in there it works fine but how do i do a range (3 Replies)
Discussion started by: rcunn87
3 Replies

10. Shell Programming and Scripting

Unix Script with line number at beginning of each line.

Could anybody help me. I need to create a script that reads a text file from STDIN and prints out the file to STDOUT with line numbers at the beginning of each line. Thanks. (5 Replies)
Discussion started by: mascorro
5 Replies
Login or Register to Ask a Question