1

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

2 Answers 2

5

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

Just remove them all and then put them back:

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.