Add double quotes in .CSV comma delimited file using awk -


hi need elaborate big csv file (20m rows) adding double quotes every comma delimited field. csv file got 8 fields coma delimited below:

'2016-03-12','12393659','134',,'35533605',189348,9798,gmail.com;live_com.com '2016-03-12','12390103','138',,'35438006',5133,1897,google.com '2016-03-12','45616164','139',,'01318800',10945593,596633,facebook.com;tumblr.com;t.co '2016-03-12','45673436','38',,'86441702',4350985,150327,serving-sys.com;chartboost.com;admarvel.com;mydas.mobi;adap.tv;cloudfront.net 

as see first 3 fields between single quotes, 4th blank, 5th between single quotes , 6th 8th comma delimited. following result (also 4th field if empty need double quoted):

"2016-03-12","12393659","134","","35533605","189348","9798","gmail.com;live_com.com" "2016-03-12","12390103","138","","35438006","5133","1897","google.com" "2016-03-12","45616164","139","","01318800","10945593","596633","facebook.com;tumblr.com;t.co" "2016-03-12","45673436","38","","86441702","4350985,"150327","serving-sys.com;chartboost.com;admarvel.com;mydas.mobi;adap.tv;cloudfront.net"   

i partially obtain result mix of sed , awk:

sed -e s/\'//g inpu.csv > output.csv eliminate quotes awk '{gsub(/[^,]+/,"\"&\"")}1' output.csv > output1.csv add double quotes 

but 4th field not double quoted , need reduce elaboration time as possible. anyway in awk better performances , 4th field double quoted. many thx help. m.tave

if data simple no embedded quotes or newlines or need is:

$ awk -f"'?,'?" -v ofs='","' '{$1=$1; gsub(/^.|$/,"\"")} 1' file "2016-03-12","12393659","134","","35533605","189348","9798","gmail.com;live_com.com" "2016-03-12","12390103","138","","35438006","5133","1897","google.com" "2016-03-12","45616164","139","","01318800","10945593","596633","facebook.com;tumblr.com;t.co" "2016-03-12","45673436","38","","86441702","4350985","150327","serving-sys.com;chartboost.com;admarvel.com;mydas.mobi;adap.tv;cloudfront.net" 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -