how to remove all double quotes from csv except specific field
5,292
This can be a way:
awk 'BEGIN{FS=OFS=","} # set input and output field separator as comma
{for (i=5; i<=NF; i++) { # loop from 5th field
gsub("\"","", $i); # remove "
gsub(/^[ \t]+/,"", $i); # remove leading spaces
gsub(/[ \t]+$/,"",$i)} # remove trailing spaces
}1' file
Removing leading and trailing is based on this answer by BMW: Remove Leading and trailing space in field in awk.
Test
$ awk 'BEGIN{FS=OFS=","} {for (i=5; i<=NF; i++) {gsub("\"","", $i); gsub(/^[ \t]+/,"", $i); gsub(/[ \t]+$/,"",$i)}}1' file
24,COsc,LINUX,"/VP/Ame/AR/Celts/COf",fbsutamante,fbu2012,kkk,&^#$@J,,,,,
25,COsc,LINUX,"/VP/Ame/AR/Celts/COf",fbsutamante,fbu2012,iiii,*****,,,,,
If it also have to clean 1st to 3rd fields, just add if (i!=4)
and loop through all the fields:
$ awk 'BEGIN{FS=OFS=","} {for (i=1; i<=NF; i++) {if (i!=4) {gsub("\"","", $i); gsub(/^[ \t]+/,"", $i); gsub(/[ \t]+$/,"",$i)}}}1' a
24,COsc,LINUX,"/VP/Ame/AR/Celts/COf",fbsutamante,fbu2012,kkk,&^#$@J,,,,,
25,COsc,LINUX,"/VP/Ame/AR/Celts/COf",fbsutamante,fbu2012,iiii,*****,,,,,
Related videos on Youtube
Comments
-
maihabunash over 1 year
I want to remove all double quotes from my csv but not the fourth field (because the four fields represent PATH of file)
Please advice how to implement this by sed or awk or perl one liner , etc
What I know for now is to use simple sed command as:
sed s"/\"//g" file.csv | sed 's/ //g'
but this command no so elegant and also work on the fourth field ( fourth field should not be edit )
Remark - need also to delete empty spaces between quotes to near character
Example ( file csv before )
"24 ","COsc ","LINUX","/VP/Ame/AR/Celts/COf"," fbsutamante ",fbu2012,"kkk","&^#$@J ",,,,, 25,COsc,LINUX,"/VP/Ame/AR/Celts/COf","fbsutamante ",fbu2012,"iiii "," *****",,,,,
Example ( file csv after after )
24,COsc,LINUX,"/VP/Ame/AR HR/Ce lts/COf",fbsutamante,fbu2012,kkk,&^#$@J,,,,, 25,COsc,LINUX,"/VP/Ame/AR HR/Ce lts/COf",fbsutamante,fbu2012,iiii,*****,,,,,
-
Stéphane Chazelas almost 10 yearsWith GNU
sed
:sed 's/\s*"\s*//3g'
-
maihabunash almost 10 yearsif you want please add this answer and I will test this
-
fedorqui almost 10 years@StéphaneChazelas note it has to apply to 1st to 3rd and 5th to the end. Does
3g
match like this? -
maihabunash almost 10 yearssee my update - yes
-
-
maihabunash almost 10 yearsone remark - sorry for this update - but quotes can be also in the first , sec , thired field ( see my update )
-
fedorqui almost 10 years@maihabunash ok, then see updated answer.