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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communitySimplest approach:
tr -d '"' <a.csv >b.csv
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.
You could use this command
awk '{gsub("\"",RS);print}' a.csv > b.csv
"
?