简体   繁体   中英

Quotation mark into .csv (per field) AWK/SED

I've got csv file like

Brand,Type,Color
Porsche,Sport,Red
BMW,Coupe,Blue

I'd like to include quotation marks to have it like:

"Brand","Type","Color"
"Porsche","Sport","Red"
"BMW","Coupe","Blue"

What's the fastest way to do it? I will implement it in cronjob.

Thanks!!!!

Adam

使用sed:

sed -e 's/^\|$/"/g' -e 's/,/","/g' input

这可能适合你(GNU sed):

sed -r 's/[^,]+/"&"/g' file

awk

awk '{gsub(/[^,]+/,"\"&\"")}1' file.csv

对于CSV数据,使用具有CSV库的语言通常更为简洁:

ruby -rcsv -ne 'puts CSV.generate_line(CSV.parse_line($_), :force_quotes=>true)'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM