Alessandro Resta
Alessandro Resta

Reputation: 1172

Regular expression - replace if character before it not x

noob question.

I would like to replace all the @ by ”,” if the character before @ is not

To increment this command cat foo.csv | sed 's/@/","/g'

Upvotes: 2

Views: 220

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626920

You can use

sed -r 's/(^|[^"])@/\1","/g'

where (^|[^"]) is a group matching either the start of a line or a character other than ". The \1 is a backreference to the Group 1 (either an empty string or a char matched with a [^"] bracket expression).

For an alternative syntax without -r, see Kent's comment (you will need to escape the special characters like (, ), | for them to behave as special regex metacharacters).

Upvotes: 2

Related Questions