Stuart Heron
Stuart Heron

Reputation: 45

Deleting characters in a CSV column with sed

I have a CSV file in with the following format:

aaxx1234a1,aaxx1234a1
aaxx1234a2,aaxx1234a2

Im trying to use sed to delete the 2 characters before the comma in the first field so as the file becomes:

aaxx1234,aaxx1234a1
aaxx1234,aaxx1234a2

I tried sed 's/.{2},//' but this didn't seem to do anything.

Can anyone give a pointer ?

Upvotes: 4

Views: 706

Answers (2)

Eric Duminil
Eric Duminil

Reputation: 54223

For this regex syntax, you need extended Regex (-r) :

sed -r 's/.{2},/,/' test.csv 

Upvotes: 1

choroba
choroba

Reputation: 241768

Sed needs the { to be backslashed to have the special meaning. Also, you don't want to remove the comma:

sed 's/.\{2\},/,/'

or, if your sed implementation supports it, switch to extended regexes by using -r or -E:

sed -E 's/.{2},/,/'

Upvotes: 4

Related Questions