Pipe sed to diff

Quiero ver el diff between a paragraph in the middle of a file and a file containing a single paragraph.

The paragraph is on line 60 de archivo fooy archivar bar contains only that paragraph with possible minor differences.

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

Las siguientes no trabajo:

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

Puedo hacer:

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?

preguntado el 27 de agosto de 12 a las 07:08

2 Respuestas

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

  sed -n 60,60p foo | diff bar -

Respondido 27 ago 12, 07:08

Y no solo diff; the pseudo-filename - signifies standard input in a lot of contexts, including, but not limited to, the GNU coreutils, Perl, and (often) Python. - triples

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)

Respondido 27 ago 12, 10:08

this is pretty useful to know - 2tazasDeTecnología

@thepickle: It does here - Thor

@Thor hmm so for me when I entered it in the terminal it worked. i create a temp.sh file. and ran the file it gave me ``` ./temp.sh: line 1: syntax error near unexpected token (' ./temp.sh: line 1: `diff <(sed -n 1,5p abc.txt) <(sed -n 1,5p def.txt)' ``` - el pepinillo

@thepickle: You are probably executing the script with dash or similar. Use bash - Thor

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.