Tarrin Skeepers
Tarrin Skeepers

Reputation: 3

Replace characters between quotes with the first character using SED

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

Answers (1)

Kent
Kent

Reputation: 195209

Give this sed one-liner a try:

sed 's/"\([^",]\+\)[^"]*"/\1/g;s/,\+/,/g' file

Explanation:

We do the substitution in two steps:

  • replace "foo,bar,baz" with foo
  • replace continuous commas with a single comma

Test

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

Related Questions