5

This question already has an answer here:

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.

marked as duplicate by countermode, user34720, Archemar, Jeff Schaller, Anthony Geoghegan Jan 19 '17 at 11:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You mean you want to delete the double quote ? This " ? – Sergiy Kolodyazhnyy Jan 19 '17 at 3:46
  • Yes exactly I want to remove the " " – joker21 Jan 19 '17 at 3:47
  • 1
    may I ask what country you're from, because I've lived in the US for 10 years, and never heard anyone refer to double quotes as inverted comma – Sergiy Kolodyazhnyy Jan 19 '17 at 3:50
  • In British English, quotation marks are called inverted commas.But anyway no worries – joker21 Jan 19 '17 at 6:20
4

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.cvs       
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($0)-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.

  • You don't need to escape the " in the regular expression; gsub(/"/,"") works. – G-Man Jan 27 '17 at 5:08
8

Simplest approach:

tr -d '"' <a.csv >b.csv
  • 1
    This is the right tool for the job. – Kusalananda Jan 19 '17 at 9:25
4

Another solution with sed:

sed -e 's/"//g' a.csv > b.csv
0

You could use this command

awk '{gsub("\"",RS);print}' a.csv >  b.csv
  • No, that doesn't work. – G-Man Jan 27 '17 at 5:05

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