Add a comma at end of every line | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Add a comma at end of every line

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-12-2002
ST2000 ST2000 is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 21 June 2005, 2:33 PM EDT
Location: Toronto
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Add a comma at end of every line

hello

A small shell scripting help..

I have a file say with 5 lines of text (text file).
At the end of everyline I need to add a comma at the end of the file.

Thanks, ST2000
Sponsored Links
    #2  
Old 07-12-2002
hell666 hell666 is offline
Registered User
 
Join Date: Jul 2002
Last Activity: 16 November 2002, 6:32 AM EST
Location: void
Posts: 53
Thanks: 0
Thanked 1 Time in 1 Post

Code:
sed "s/$/,/g" filename

Sponsored Links
    #3  
Old 07-12-2002
ST2000 ST2000 is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 21 June 2005, 2:33 PM EDT
Location: Toronto
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Bug

Thanks a lot ..

I can only display a comma at the end of line and pipe it to new file, but how can i do that inside the file and retain the same name as well.. When I do on same file, it will simply erase it.

Moreover I dont need the comma only in the last line..

Thanks, ST2000
    #4  
Old 07-12-2002
pipin pipin is offline
Registered User
 
Join Date: Jul 2002
Last Activity: 15 July 2002, 2:08 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ed filename <<EOF
%s/$/,/
$
s/,$//
w
q
EOF

Not sure, cannot test it right now ...

Last edited by pipin; 07-15-2002 at 02:08 AM..
Sponsored Links
    #5  
Old 07-14-2002
peter.herlihy peter.herlihy is offline
Registered User
 
Join Date: Nov 2001
Last Activity: 1 August 2006, 11:51 AM EDT
Location: New Zealand
Posts: 333
Thanks: 0
Thanked 0 Times in 0 Posts
You may be familiar with using 'vi'...in which case you can open a file using vi...manipulate as required then save the changes and you have amended the file.

You can actually do this both from the command line or using 'vi' or another editor like ed. From the command line you will have to direct output to another file and then rename the file back....this code would do it for you...

sed -e 's/$/,/g' -e '$ s/,$//' your_file > new_file && mv new_file your_file
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to grep after the first comma till the next comma in a line rxg Shell Programming and Scripting 8 05-11-2012 11:24 AM
Adding comma at the end of every line jakSun8 UNIX for Dummies Questions & Answers 2 04-21-2012 08:42 PM
How to remove comma from the last line of the file sandeep_1105 UNIX for Dummies Questions & Answers 5 05-27-2009 12:01 PM
Second comma to new line. deepakwins UNIX for Dummies Questions & Answers 3 02-14-2007 01:35 AM
need to get the last word in comma sep line senthilk615 Shell Programming and Scripting 9 03-31-2006 12:31 AM



All times are GMT -4. The time now is 05:53 PM.