The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extract a line from a file using the line number zambo Shell Programming and Scripting 1 05-01-2008 10:39 AM
Appending line number to each line and getting total number of lines chiru_h Shell Programming and Scripting 2 03-25-2008 06:19 AM
extract a line from a file using the line number grandtheftander Shell Programming and Scripting 6 02-06-2008 02:12 AM
Appending a line in a file after a particular line maxvirrozeito Shell Programming and Scripting 7 12-12-2007 09:58 AM
Get line form file by line number corny Shell Programming and Scripting 3 08-25-2006 09:18 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2007
Registered User
 

Join Date: Mar 2007
Location: Sydney, Australia
Posts: 2
Unhappy 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 .

This is on a Sun Solaris box btw.

Any help appreciated.

Thanks

Paul
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-20-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
Code:
nawk -v OFS=';' '{print $0, FNR}' myFile
Reply With Quote
  #3 (permalink)  
Old 03-20-2007
Registered User
 

Join Date: Mar 2007
Location: Sydney, Australia
Posts: 2
Thanks very much vgersh99 , works great.
Reply With Quote
  #4 (permalink)  
Old 03-20-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
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
Reply With Quote
  #5 (permalink)  
Old 03-20-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,454
Code:
cat -n filename |sed 's/\(.*\)\([0-9][0-9]*\)\(.*\)/\3;\2/'
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:37 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0