Sed replace only lines with matching groups

sed

Currently, I have this command:

sed 's/\([^C]*\)/\1AB/'

It inserts "AB" before the first instance of "C" on a line. But it is also adding "AB" at the end of every line without a "C".

How can I have it only edit when it finds "C"?

Best Answer

What's wrong with simply replacing the C?

sed 's/C/ABC/'

This will only replace the first instance (if you wanted to replace them all, you would add g at the end).

Related Question