Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-07-2012
Registered User
 
Join Date: Aug 2011
Posts: 50
Thanks: 0
Thanked 1 Time in 1 Post
insert comma

my file looks like this:
297 PC Closed 07/10/12 999000098 AMERICAN SOCIETY FOR HEALTHCAR 0.00 USD 1 NAI

i want to look line this:
297,PC,Closed,07/10/12,999000098,AMERICAN SOCIETY FOR HEALTHCAR,0.00,USD,1,NAI
Sponsored Links
    #2  
Old 09-07-2012
Registered User
 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Try this:
cat "File Name" | sed -e 's/ /,/g'
Sponsored Links
    #3  
Old 09-07-2012
Registered User
 
Join Date: Mar 2012
Location: Pune, India
Posts: 1,485
Thanks: 56
Thanked 425 Times in 421 Posts
Quote:
Originally Posted by Tarek EzzElDin View Post
Try this:
cat "File Name" | sed -e 's/ /,/g'
that's useless use of cat

With your command it replaces all the spaces with ",". which is not desired output.
see the output below..

Code:
$ echo "297 PC Closed 07/10/12 999000098 AMERICAN SOCIETY FOR HEALTHCAR 0.00 USD 1 NAI" | sed -e 's/ /,/g'
297,PC,Closed,07/10/12,999000098,AMERICAN,SOCIETY,FOR,HEALTHCAR,0.00,USD,1,NAI

Hi lawsongeek,

Could you provide some more lines from your input. Is Columns are tab separated?
    #4  
Old 09-07-2012
Registered User
 
Join Date: Aug 2011
Posts: 50
Thanks: 0
Thanked 1 Time in 1 Post
5016487 RS Issued 08/22/12 7621 MOLECULAR DEVICES INC 4500.00 USD 1 NAI
5016488 RS Issued 08/23/12 7621 MOLECULAR DEVICES INC 2000.00 USD 1 NAI
5016490 RS Issued 09/04/12 22050 WUXI APPTEC, INC 3700.00 USD 1 NAI
Sponsored Links
    #5  
Old 09-07-2012
Registered User
 
Join Date: Mar 2012
Location: Pune, India
Posts: 1,485
Thanks: 56
Thanked 425 Times in 421 Posts
Quote:
Originally Posted by lawsongeek View Post
5016487 RS Issued 08/22/12 7621 MOLECULAR DEVICES INC 4500.00 USD 1 NAI
5016488 RS Issued 08/23/12 7621 MOLECULAR DEVICES INC 2000.00 USD 1 NAI
5016490 RS Issued 09/04/12 22050 WUXI APPTEC, INC 3700.00 USD 1 NAI
Assuming you have similar pattern for all the inputs..

Try something this....


Code:
awk '{for (i=6;i<=(NF-4);i++){if(p){p=p" "$i}else{p=$i}}{print $1,$2,$3,$4,$5,p,$(NF-3),$(NF-2),$(NF-1),$NF;p=""}}' OFS=\, 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
Insert comma in place of column manigrover Shell Programming and Scripting 5 08-13-2012 02:29 AM
Script using Sed :Search all patterns & after the last Patter, insert a newLine with Comma Sep Value evrurs Shell Programming and Scripting 8 08-01-2012 12:49 PM
Need to remove comma from description in Comma separated csv file ops_team Shell Programming and Scripting 11 03-22-2012 06:40 AM
insert comma in a text file GoldenFire UNIX for Dummies Questions & Answers 7 12-15-2010 11:21 AM
Insert comma based on max number of column nicholas_ejn Shell Programming and Scripting 8 12-11-2008 01:08 PM



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