Appending the line number and a seperator to each line of a file ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending the line number and a seperator to each line of a file ?
# 1  
Old 03-20-2007
Data Appending the line number and a seperator to each line of a file ?

Hi, I am a newb as far as shell scripting and SED goes so bear with me on this one.

I want to basically append to each line in a file a delimiter character and the line's line number e.g

Change the file from :-

aaaaaa
bbbbbb
cccccc

to:-

aaaaaa;1
bbbbbb;2
cccccc;3

I have worked out how to append the ";" to the end of each line ( sed '1,$s/$/;/') but am stumped with the line number bit Smilie .

This is on a Sun Solaris box btw.

Any help appreciated.

Thanks

Paul
# 2  
Old 03-20-2007
Code:
nawk -v OFS=';' '{print $0, FNR}' myFile

# 3  
Old 03-20-2007
Thanks very much vgersh99 Smilie , works great.
# 4  
Old 03-21-2007
Code:
$ sed = file | sed "N;s/\(.*\)\n\(.*\)/\2;\1/"
aaaaaa;1
bbbbbb;2
cccccc;3
$ sed -n "p;=" file | sed "N;s/\n/;/"
aaaaaa;1
bbbbbb;2
cccccc;3

# 5  
Old 03-21-2007
Code:
cat -n filename |sed 's/\(.*\)\([0-9][0-9]*\)\(.*\)/\3;\2/'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Hos to get the line number of uncommented line from the file

I have few lines in a text file. I am trying to get the line number of uncommented line from the text file using unix shell script. For example : I want the line number of Orange from the below text file. Here expected answer is 4 since the line 2 is commented. Apple #Orange grapes Orange (4 Replies)
Discussion started by: Brennon
4 Replies

2. 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

3. 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

4. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

5. Shell Programming and Scripting

Increasing a number and appending it to next line of a text file

Hi all, I have text file having a number P100. what i need is when i run a script, it should add 1 to the above number and append it to the next line of a same text file.. when i use the script next time it should check the last line and add 1 to the last number and so on.. like the text... (5 Replies)
Discussion started by: smarty86
5 Replies

6. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

7. Shell Programming and Scripting

sed: appending alternate line after previous line

Hi all, I have to append every alternate line after its previous line. For example if my file has following contents line 1: unix is an OS line 2: it is open source line 3: it supports shell programming line 4: we can write shell scripts Required output should be line1: unix is an OS it is... (4 Replies)
Discussion started by: rish_max
4 Replies

8. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

9. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies

10. Shell Programming and Scripting

Appending a line in a file after a particular line

Hello, I have got a C file in which I would like to add an include statement of my own. There are already a few include statements and mine should come right after the last existing one (to be neat). With grep I can get the lines containing the word 'include' and I guess I should feed the... (7 Replies)
Discussion started by: maxvirrozeito
7 Replies
Login or Register to Ask a Question