0

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,*****,,,,,
  • With GNU sed: sed 's/\s*"\s*//3g' – Stéphane Chazelas Jul 29 '14 at 10:19
  • if you want please add this answer and I will test this – maihabunash Jul 29 '14 at 10:21
  • @StéphaneChazelas note it has to apply to 1st to 3rd and 5th to the end. Does 3g match like this? – fedorqui Jul 29 '14 at 10:23
  • see my update - yes – maihabunash Jul 29 '14 at 10:25
4

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,*****,,,,,
  • one remark - sorry for this update - but quotes can be also in the first , sec , thired field ( see my update ) – maihabunash Jul 29 '14 at 10:23
  • @maihabunash ok, then see updated answer. – fedorqui Jul 29 '14 at 10:25
0

Just remove them all and then put them back:

sed 's/"//g;s/,/,"/3;s/,/",/4'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.