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,*****,,,,,
Share:
5,292

Related videos on Youtube

maihabunash
Author by

maihabunash

I am 17 years old and love to develop

Updated on September 18, 2022

Comments

  • maihabunash
    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
      Stéphane Chazelas almost 10 years
      With GNU sed: sed 's/\s*"\s*//3g'
    • maihabunash
      maihabunash almost 10 years
      if you want please add this answer and I will test this
    • fedorqui
      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
      maihabunash almost 10 years
      see my update - yes
  • maihabunash
    maihabunash almost 10 years
    one remark - sorry for this update - but quotes can be also in the first , sec , thired field ( see my update )
  • fedorqui
    fedorqui almost 10 years
    @maihabunash ok, then see updated answer.