Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert spaces to commas

Status
Not open for further replies.

ryanc2

MIS
Apr 18, 2003
73
US
I need a simple awk or sed script to convert a text file with columns seperated by spaces to output to a .csv file.

Any help would be appreciated.

Thanks
 
sed -e 's/[ ][ ]*/,/g' file
nawk -v OFS=',' '$1=$1'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks for the input.

Wonboodoo - How can I put quotes around so Excel will inport as text field and not strip leading zeroes? Also, is there a way to eliminate putting a comma before the first column (ends up importing a blank column in Excel).

Thanks again!
 
sed -e 's/^[ ][ ]*//g;s/[ ][ ]*/,/g;s/[^,]/\&quot;&\&quot;/g' file

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
or with awk:

nawk -v OFS='&quot;,&quot;' '{$1=&quot;\&quot;&quot;$1; $NF=$NF &quot;\&quot;&quot;; print}' file

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
sed -e 's/^[ ][ ]*//g;s/[ ][ ]*/,/g;s/[^,]/\&quot;&\&quot;/g' file

Vlad - This puts quotes around every character. What about each column:

12345,Name,12 --> &quot;12345&quot;,&quot;Name&quot;,&quot;12&quot;

Thanks again.

 
sed -e 's/^[ ][ ]*//g;s/[ ][ ]*/,/g;s/[^,][^,]*/\&quot;&\&quot;/g' file

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top