Reputation: 3
I need help with the following:
I have a csv with rows like:
a,b,c,d,"e,f,g",h,i
j,"k,l",m,n,"o,p",q
I need to select all the rows but only include the first character between the quotes. The result should look like:
a,b,c,d,e,h,i
j,k,m,n,o,q
This is for a shell script. So far I only have this:
sed 's/".*"//' xxx.csv
which gives
a,b,c,d,,h,i
j,,q
I also tried
sed 's/"// ; s/,.*"//' xxx.csv
and got this
a,h,i
j,q
SO I am not sure what to add in the replace section of the SED command to keep the characters I need.
Upvotes: 0
Views: 144
Reputation: 195209
Give this sed one-liner a try:
sed 's/"\([^",]\+\)[^"]*"/\1/g;s/,\+/,/g' file
We do the substitution in two steps:
"foo,bar,baz"
with foo
With your example:
kent$ cat f
a,b,c,d,"e,f,g",h,i
j,"k,l",m,n,"o,p",q
kent$ sed 's/"\([^",]\+\)[^"]*"/\1/g;s/,\+/,/g' f
a,b,c,d,e,h,i
j,k,m,n,o,q
Upvotes: 3