Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add quotation marks to fields in a CSV file?

Tags:

csv

sed

awk

I've got a 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 a cronjob.

like image 488
Adam Lesniak Avatar asked Jun 17 '13 22:06

Adam Lesniak


2 Answers

Using sed:

sed -e 's/^\|$/"/g' -e 's/,/","/g' input
like image 69
perreal Avatar answered Sep 22 '22 14:09

perreal


This might work for you (GNU sed):

sed -r 's/[^,]+/"&"/g' file
like image 42
potong Avatar answered Sep 18 '22 14:09

potong