6

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"?

4

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).

3

You need an address filter:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.