Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

 
discard

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.