Use pipe of commands as argument for diff
The simplest approach is:
grep -E '^[0-9]+$' file | diff file -
The hyphen -
as the filename is a specific notation that tells diff
"use standard input"; it's documented in the diff
man-page. (Most of the common utilities support the same notation.)
The reason that backticks don't work is that they capture the output of a command and pass it as an argument. For example, this:
cat `echo file`
is equivalent to this:
cat file
and this:
diff file "`cat file | grep -E ^[0-9]+$`"
is equivalent to something like this:
diff file "123
234
456"
That is, it actually tries to pass 123234345
(plus newlines) as a filename, rather than as the contents of a file. Technically, you could achieve the latter by using Bash's "process substitution" feature that actually creates a sort of temporary file:
diff file <(cat file | grep -E '^[0-9]+$')
but in your case it's not needed, because of diff
's support for -
.
grep -E '^[0-9]+$' file | diff - file
where -
means "read from standard input".
If you're using bash:
diff file <(grep -E '^[0-9]+$' file)
The <(COMMAND)
sequence expands to the name of a pseudo-file (such as /dev/fd/63
) from which you can read the output of the command.
But for this particular case, ruakh's solution is simpler. It takes advantage of the fact that -
as an argument to diff
causes it to read its standard input. The <(COMMAND)
syntax becomes more useful when both arguments to diff
are command output, such as:
diff <(this_command) <(that_command)