Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

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

share|improve this answer

You need an address filter:

sed '/C/s/\([^C]*\)/\1AB/'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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