![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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 |
|
||||
|
Quote:
Code:
awk '{printf("%d,%d,%d,\"%d %d\",\"%d %d\",%d,%d\n",$1,$2,$3,$4,$5,$6,$7,$8,$9)}'
Regards, |
|
||||
|
Quote:
![]() Cheers |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|