Converting spaces to csv


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Converting spaces to csv
# 1  
Old 06-15-2009
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  
Old 06-15-2009
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  
Old 06-15-2009
No need for the cat

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

# 4  
Old 06-15-2009
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  
Old 06-15-2009
Quote:
Originally Posted by Franklin52
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  
Old 06-15-2009
Quote:
Originally Posted by rakeshawasthi
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! Smilie

Cheers
# 7  
Old 06-15-2009
Quote:
Originally Posted by rakeshawasthi
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting XML to CSV

Hello, For i while i have been using XMLStarlet to convert several XML files to CSV files. So far this always went fine. Today however i got a new XML format however but i cannot find out how to get the data i need. Below is part of the code where it shows the different format. What... (10 Replies)
Discussion started by: SDohmen
10 Replies

2. Shell Programming and Scripting

Converting csv to html format

Below is the code I have - How can I convert the data in the csv into 3 tables in html. instead of 1 table. Attached is the format I am getting. (1 Reply)
Discussion started by: archana25
1 Replies

3. Shell Programming and Scripting

Converting CSV to ascii format

Hej All, I have a file like this which is a comma dilimited: input: 6000318,-263.011520678,-59.05869872409,587.67924868792 6000319,-265.6996842902,-50.24902479999,590.65693082607 6000320,-238.1333898366,-288.801232595,633.75332173496... (5 Replies)
Discussion started by: Johanni
5 Replies

4. UNIX for Dummies Questions & Answers

cleaning up spaces from fixed width file while converting to csv file

Open to a sed/awk/or perl alternative so that i can stick command into my bash script. This is a problem I resolve using a combination of cut commands - but that is getting convoluted. So would really appreciate it if someone could provide a better solution which basically replaces all... (3 Replies)
Discussion started by: svn
3 Replies

5. Shell Programming and Scripting

Converting txt file in csv

HI All, I have a text file memory.txt which has following values. Average: 822387 7346605 89.93 288845 4176593 2044589 51883 2.47 7600 i want to convert this file in csv format and i am using following command to do it. sed s/_/\./g <... (3 Replies)
Discussion started by: mkashif
3 Replies

6. Shell Programming and Scripting

Converting 2 or more spaces to a single tab

I'm new to bash and want to know a simple sed, awk, or grep script that will find all instances of 2 or more spaces and convert them to a single tab. Thanks for the help in advance. (1 Reply)
Discussion started by: jkandel
1 Replies

7. UNIX for Dummies Questions & Answers

Converting csv to tsv and back

Anyone have a simple UNIX script to look at for doing this? I'm interested in how to convert from csv to tsv, and then back again, if I want to. Thanks! (13 Replies)
Discussion started by: doubleminus
13 Replies

8. UNIX for Dummies Questions & Answers

Converting HTML to CSV

Hi, I need to convert a relatively large html file (1.5megs) into CSV under Unix. How would I be able to do this? Much thanks. (3 Replies)
Discussion started by: Jexel
3 Replies

9. Shell Programming and Scripting

Converting csv to xls

Hi, Can anyone tell the option to change the file type in unix. i.e. if a file is in csv(Comma Separating Values) format, it should be changed to xls(ordinary MS-Excel) format. But renaming command is not changing to correct file format. Thanks in advance, Milton. (1 Reply)
Discussion started by: miltony
1 Replies

10. Shell Programming and Scripting

Converting tabs in to spaces.

Hi! I'm using SunOS 5.7 w/ Bash 2.01. Currently, I'm working on a script that will make it possible to find textfiles which match certain criteria. While I write this message, I had some brainfarts, found the answer myself :D and the question I had in mind is now no longer the question I... (3 Replies)
Discussion started by: indo1144
3 Replies
Login or Register to Ask a Question