Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

2 Answers 2

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
add comment

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
add comment

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.