开发者

Tab Delimited to Comma Separated file in UNIX - One liner?

开发者 https://www.devze.com 2022-12-30 00:50 出处:网络
I have a tab delimited file (MySQL Out file). I want to convert it into CSV file. I got everything working except for replacing NULLs to nothing or spaces.

I have a tab delimited file (MySQL Out file).

I want to convert it into CSV file. I got everything working except for replacing NULLs to nothing or spaces.

What I have is : sed -e 's/^/"/; s/$/"/; s/\t/","/g;' < file.csv > file1.csv

How to also replace NULLs in the s开发者_如何学JAVAame line.

The following doesn't work.

sed -e 's/NULL//; s/^/"/; s/$/"/; s/\t/","/g;' < file.csv > file1.csv

Any input is much appreciated.

Thank you!


you can't do this with sed.

#!/usr/bin/env python

import sys, csv

reader = csv.reader(open(sys.argv[1], 'rb'), delimiter="\t", escapechar="\\")
writer = csv.writer(open('output.csv', 'wb'))
for row in reader:
    writer.writerow(row)


Didn't you just forget to put the 'g'-modifier in there?

sed -e 's/NULL//g; s/^/"/; s/$/"/; s/\t/","/g;' < file.csv > file1.csv
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号