The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to append spaces(say 10 spaces) at the end of each line based on the length of th prathima UNIX for Dummies Questions & Answers 17 01-28-2009 04:10 PM
AIX converting PDF to PS vinayakshukre AIX 3 12-11-2008 06:14 AM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 02:24 PM
converting .txt laila63 UNIX for Dummies Questions & Answers 4 06-30-2004 05:56 PM
Converting tabs in to spaces. indo1144 Shell Programming and Scripting 3 06-28-2002 06:56 AM

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 06-15-2009
jkandel jkandel is offline
Registered User
  
 

Join Date: May 2009
Posts: 6
Converting spaces to csv

I'm new to shell scripting and I have a file with positional columns. The rows look like this:

222 3333 44444 55 55 55 55 5555 59999
222 3333 44444 55 55 55 55 5555 59999

Not every row has the same number of spaces between the seven columns. They can range anywhere from one space to seven, for instance. But ultimately, I want the file to be a clean .csv:

222,3333,44444,"55 55","55 55",5555,59999
222,3333,44444,"55 55","55 55",5555,59999

How might I accomplish this? Any help would be much appreciated.
  #2 (permalink)  
Old 06-15-2009
robsonde robsonde is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 163
using sed to do a search relpace...


Code:
# cat file | sed -e 's/ */,/g' > newfile

so we have relace one or more spaces with a ,

I have not tested this, it's just off the top of my head...
  #3 (permalink)  
Old 06-15-2009
BubbaJoe's Avatar
BubbaJoe BubbaJoe is offline
Registered User
  
 

Join Date: Oct 2008
Location: St Louis
Posts: 153
No need for the cat


Code:
sed -e 's/ */,/g' file > newfile

  #4 (permalink)  
Old 06-15-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
None of the solutions above don't give the desired output!

Try this, the awk command loops through the fields 4 until 7 and adds a double quote before the even fields and one after the odd fields:


Code:
 awk '{for(i=4;i<=7;i++){$i=i%2?$i"\"":"\""$i}}1' file

Regards
  #5 (permalink)  
Old 06-15-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
Quote:
Originally Posted by Franklin52 View Post
None of the solutions above don't give the desired output!

Try this, the awk command loops through the fields 4 until 7 and adds a double quote before the even fields and one after the odd fields:


Code:
 awk '{for(i=4;i<=7;i++){$i=i%2?$i"\"":"\""$i}}1' file

Regards
Franklin52, Why cant I keep it simple like this....

Code:
awk '{printf("%d,%d,%d,\"%d %d\",\"%d %d\",%d,%d\n",$1,$2,$3,$4,$5,$6,$7,$8,$9)}'

Regards,
  #6 (permalink)  
Old 06-15-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
Quote:
Originally Posted by rakeshawasthi View Post
Franklin52, Why cant I keep it simple like this....

Code:
awk '{printf("%d,%d,%d,\"%d %d\",\"%d %d\",%d,%d\n",$1,$2,$3,$4,$5,$6,$7,$8,$9)}'

Regards,
No problem with it, everyone has his own taste, I like it spicy an hot!

Cheers
  #7 (permalink)  
Old 06-15-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,560
Quote:
Originally Posted by rakeshawasthi View Post
Franklin52, Why cant I keep it simple like this....
because you might never know there will be more fields in the future. using the for loop is easier to modify next time.
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 06:23 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