The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 grandtheftander Shell Programming and Scripting 7 01-22-2009 10:24 AM
Extract a line from a file using the line number zambo Shell Programming and Scripting 1 05-01-2008 01:39 PM
Appending line number to each line and getting total number of lines chiru_h Shell Programming and Scripting 2 03-25-2008 10:19 AM
Appending a line in a file after a particular line maxvirrozeito Shell Programming and Scripting 7 12-12-2007 01:58 PM
Get line form file by line number corny Shell Programming and Scripting 3 08-25-2006 12:18 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-20-2007
pjcwhite pjcwhite is offline
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
  #2 (permalink)  
Old 03-20-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
nawk -v OFS=';' '{print $0, FNR}' myFile
  #3 (permalink)  
Old 03-20-2007
pjcwhite pjcwhite is offline
Registered User
  
 

Join Date: Mar 2007
Location: Sydney, Australia
Posts: 2
Thanks very much vgersh99 , works great.
  #4 (permalink)  
Old 03-21-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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 (permalink)  
Old 03-21-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Code:
cat -n filename |sed 's/\(.*\)\([0-9][0-9]*\)\(.*\)/\3;\2/'
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0