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
perl help for comma seperated output namishtiwari Shell Programming and Scripting 3 06-15-2009 05:21 AM
Removing blank lines from comma seperated and space seperated file. pinnacle Shell Programming and Scripting 11 05-08-2009 12:58 PM
change list to comma seperated items insania Shell Programming and Scripting 2 10-03-2007 06:21 PM
Comma Seperated List of Values brap45 Shell Programming and Scripting 9 02-23-2006 05:12 AM
Handling .CSV( Comma seperated value) in awk Laud12345 Shell Programming and Scripting 1 05-09-2005 05:41 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-24-2009
Tuxidow Tuxidow is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 47
Script to append a value seperated by comma

I have a file like below. How can I add a new value after moss separated by a comma. This adding script should work in such a way that each time i pass a value it should be added one after the other separated by commas.

Code:
hru:122:
hello:123:john,philip,mary,kp,moss
hi:124:
bye:125:
Can anybody please help me in this.
  #2 (permalink)  
Old 08-24-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
depending on what you want to do, this might be one of them:
Code:
nawk -v val='foo' '!/:$/ {$0=$0 "," val}1' myFile
  #3 (permalink)  
Old 08-25-2009
Tuxidow Tuxidow is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 47
thanks a lot for the reply!!

i want this to add only for "hello".. how to do that...? not generic..!! can anybody please help me to understand the above code too??

---------- Post updated at 10:46 PM ---------- Previous update was at 06:47 AM ----------

myfile
Code:
hru:122:
hello:123:john,philip,mary,kp,moss
hi:124:
bye:125:
hi:124:
bye:125:
hi:124:
bye:125:
tata:125:pipe,grep
How to modify the above script just to add the new value only to "hello" not for tata. Script should be some what like, search for hello and add the new value there. Each new values to be separated by a comma.. Sorry for not being so clear in my first post

So if the new value i have is foo, the modified file should be like this..

Code:
hru:122:
hello:123:john,philip,mary,kp,moss,foo
hi:124:
bye:125:
hi:124:
bye:125:
hi:124:
bye:125:
tata:125:pipe,grep
I modified the script like this..

Code:
awk -v val='foo' '!/:$/ {$0=$0 "," val}1' myfile > myfilenew
cat myfilenew > myfile
rm -rf myfilenew
bu the output im getting is like the following...

the new myfile
Code:
hru:122:
hello:123:john,philip,mary,kp,moss,foo
hi:124:
bye:125:
hi:124:
bye:125:
hi:124:
bye:125:
tata:125:pipe,grep,foo
It would be a great help if some one can help me in this.

Last edited by Tuxidow; 08-25-2009 at 03:46 AM..
  #4 (permalink)  
Old 08-25-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,309
Have you tried to adjust the code?

Regards
  #5 (permalink)  
Old 08-25-2009
Tuxidow Tuxidow is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 47
I am not so good in awk. hence not able to understand the above code...!! :-(. hence could not modify it... :-(
  #6 (permalink)  
Old 08-25-2009
thanhdat's Avatar
thanhdat thanhdat is offline
Registered User
  
 

Join Date: Aug 2008
Location: Paris
Posts: 107
so, take a quick look at this link: 10 Awk Tips, Tricks and Pitfalls - good coders code, great reuse
hope that helps!
  #7 (permalink)  
Old 08-25-2009
Tuxidow Tuxidow is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 47
I got it...!! Thanks for all your help...!!


Code:

awk -v val='foo' '/hello:123:/ {$0=$0 "," val}1' myFile
Reply

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 04:23 PM.


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