Thursday 10 November 2005

Use unix 'sed' to re-format csv files

Suppose you have a csv file which contains text like this

...
aaaa@bCC.com
cccc@dddddd.com.tr
lllll@kkkk.de
...

and you want to convert it to a CSV file so that you can use it in SQL,
something like this:

...
'aaaa@bCC.com',
'cccc@dddddd.com.tr',
'lllll@kkkk.de',
...


You can use a sed on unix to do it like this.

$ sed -e "/^$/d" -e "s/^/\'/" -e "s/$/\',/" textfile.txt > newfile.txt