Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe sed to diff

Tags:

macos

diff

sed

pipe

I want to see the diff between a paragraph in the middle of a file and a file containing a single paragraph.

The paragraph is on line 60 of file foo, and file bar contains only that paragraph with possible minor differences.

I can extract that paragraph using sed thusly: sed -n 60,60p foo. How can I use this in diff?

The following don't work:

sed -n 60,60p foo | diff bar # diff: missing operand after `foo`
diff bar `sed -n 60,60p foo` # diff: extra operand `in`

I can do:

sed -n 60,60p foo >> tempfile; diff bar tempfile

Is there a solution that doesn't require me to store somewhere temporarily using a pipe?

like image 710
simont Avatar asked Aug 27 '12 06:08

simont


2 Answers

If you use a '-' as file argument, diff will read from stdin:

  sed -n 60,60p foo | diff bar -
like image 173
Matthias Avatar answered Nov 08 '22 12:11

Matthias


You could use process substitution:

diff bar <(sed -n 60,60p foo)

This can also be used to compare the output from two processes:

diff <(sed -n 60,60p bar) <(sed -n 60,60p foo)
like image 25
Thor Avatar answered Nov 08 '22 12:11

Thor