7

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.

4
  • You mean you want to delete the double quote ? This " ? Jan 19, 2017 at 3:46
  • Yes exactly I want to remove the " "
    – joker21
    Jan 19, 2017 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 Jan 19, 2017 at 3:50
  • In British English, quotation marks are called inverted commas.But anyway no worries
    – joker21
    Jan 19, 2017 at 6:20

4 Answers 4

10

Simplest approach:

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

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.

0
5

Another solution with sed:

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

You could use this command

awk '{gsub("\"",RS);print}' a.csv >  b.csv
1

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