Home > front end >  Need to replace comma delimiter in a file with a double quote for only certain texts in unix [closed
Need to replace comma delimiter in a file with a double quote for only certain texts in unix [closed

Time:09-17

i have below text in a file and need to only add "" to those strings which are not enclosed in double quote already and ignore if strings are already enclosed in "".

input eg :-

"1235331315","2080N0001X",33146-2423,2008-06-13,2299-12-31,A,"SANDEEP"

i need the output as below

"1235331315","2080N0001X","33146-2423","2008-06-13","2299-12-31","A","SANDEEP"

CodePudding user response:

Using csvformat from the handy csvkit package:

csvformat -U1 input.csv

CodePudding user response:

you can use awk & to print double-quotes, you can add \x22.

-bash-4.2$ cat list.txt
"1235331315","2080N0001X",33146-2423,2008-06-13,2299-12-31,A,"SANDEEP"
-bash-4.2$ awk -F"," '{print $1","$2",""\x22"$3"\x22"",""\x22"$4"\x22"",""\x22"$5"\x22"",""\x22"$6"\x22"","$7}' list.txt
"1235331315","2080N0001X","33146-2423","2008-06-13","2299-12-31","A","SANDEEP"
-bash-4.2$ 
  • Related