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
link|improve this question
feedback

2 Answers

This is specific to GNU sed:

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

You could use /dev/stderr instead.

link|improve this answer
feedback

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 '^@'
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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