Server Fault is a question and answer site for system and network administrators. 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

I have a simple sed expression:

sed -i 's/foo/bar/g' blat.xml

Because the expression modifies the file in place it is hard to see which lines were changed.

Is there a way to echo the modified lines to the console?

Something similar to the output from the following would be ideal:

sed -n 's/foo/bar/gp' blat.xml
share|improve this question

This is specific to GNU sed:

sed -i 's/foo/bar/gw /dev/stdout' blat.xml

You could use /dev/stderr instead.

share|improve this answer

Not elegant, but usable without gnu sed. We give up on modifying the file in-place, and assume none of the lines start with "@".

sed 's/foo/bar\n@bar/' infile | tee outfile.tmp | grep '^@'
grep -v '^@' outfile.tmp > outfile

You can do it all in one step with bash, perhaps with other shells as well:

sed 's/foo/bar\n@bar/' infile | tee >(grep -v '^@' >outfile) | grep '^@'
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.