how to remove the double quotes in a csv
Solution 1
Use gsub()
function for global substitution
$ awk '{gsub(/"/,"")};1' input.csv
1,2,3,4,9
1,2,3,6,24
1,2,6,8,28
1,2,4,6,30
To send output to new file use >
shell operator:
awk '{gsub(/"/,"")};1' input.csv > output.csv
Your splitting to array approach also can be used, although it's not necessary, but you can use it as so:
$ awk '{split($0,a,/"/); print a[2]}' input.csv
1,2,3,4,9
1,2,3,6,24
1,2,6,8,28
1,2,4,6,30
Note that in this particular question the general pattern is that quotes are in the beginning and end of the line, which means we can also treat that as field separator, where field 1 is null, field 2 is 1,2,3,4
, and field 3 is also null. Thus, we can do:
awk -F '"' '{print $2}' input.csv
And we can also take out substring of the whole line:
awk '{print substr($0,2,length()-2)}' quoted.csv
Speaking of stripping first and last characters, there's a whole post on stackoverflow about that with other tools such as sed
and POSIX shell.
Solution 2
Simplest approach:
tr -d '"' <a.csv >b.csv
Solution 3
Another solution with sed
:
sed -e 's/"//g' a.csv > b.csv
Solution 4
You could use this command
awk '{gsub("\"",RS);print}' a.csv > b.csv
Related videos on Youtube
joker21
Updated on September 18, 2022Comments
-
joker21 over 1 year
I have a CSV file like
a.csv "1,2,3,4,9" "1,2,3,6,24" "1,2,6,8,28" "1,2,4,6,30"
I want something like
b.csv 1,2,3,4,9 1,2,3,6,24 1,2,6,8,28 1,2,4,6,30
I tried
awk '{split($0,a,"\"");
But did not help.Any help is appreciated.-
Sergiy Kolodyazhnyy over 7 yearsYou mean you want to delete the double quote ? This
"
? -
joker21 over 7 yearsYes exactly I want to remove the " "
-
joker21 over 7 yearsIn British English, quotation marks are called inverted commas.But anyway no worries
-
-
Kusalananda over 7 yearsThis is the right tool for the job.
-
G-Man Says 'Reinstate Monica' over 7 yearsNo, that doesn't work.