2

Is it possible to diff the output of two grep commands?

I am currently searching for files, using different exclude patterns, and the output it pretty long, so i wanted to see if my new pattern worked, or the output is still the same..

Is it possible to somehow pipe two grep command into a diff or something like that?

grep --exclude=*{.dll, pdb} --ril "dql"
grep  --ril "dql"
3
  • Redirect the grep output into 2 files and then diff the files.
    – DavidPostill
    Commented Apr 2, 2018 at 11:18
  • Please, read MCVE Commented Apr 2, 2018 at 11:18
  • @DavidPostill That was my initial idea, but wanted to know if there was a neater "trick" rather than creating temp files, and diff those. Commented Apr 2, 2018 at 11:23

2 Answers 2

4

Using on the fly :

diff <(grep pattern file) <(grep another_pattern file)

Process Substitution: <(command) or >(command) is replaced by a FIFO or /dev/fd/* entry. Basically shorthand for setting up a named pipe. See http://mywiki.wooledge.org/ProcessSubstitution.
Example: diff -u <(sort file1) <(sort file2)

So :

diff <(grep --exclude=*{.dll, pdb} --ril "dql") <(grep  --ril "dql")
4
  • hmmm... I am getting error message diff: extra operand /dev/fd/61'` Commented Apr 2, 2018 at 11:37
  • -bash: 0: ambiguous redirect Commented Apr 2, 2018 at 11:44
  • I think you made a typo Commented Apr 2, 2018 at 12:07
  • not sure where though.. diff <(grep --exclude=*.{dll,pdb,svg,DS_Store,exe,zip,tdf,Oracle,sql,cache,txt,cfs,fnm,log,css,xdt,psm1} -ril "del" .) <(grep --exclude=*.{dll,pdb,svg,DS_Store,exe,zip,tdc,Oracle,sql,cache,txt,cfs,fnm,log,css,xdt,psm1} -ril "del" .) Commented Apr 2, 2018 at 12:13
1

In bash using process substitution:

$ echo a > FILE
$ echo b > FILE1
$ diff <(grep a *) <(grep b *)
1c1
< FILE:a
---
> FILE1:b

As describen in man bash:

   Process Substitution
   Process substitution is supported on systems that support named
   pipes (FIFOs) or the /dev/fd method of naming open files.  It
   takes the form of <(list) or >(list).  The process list is run
   with its input or output connected to a FIFO or some file in
   /dev/fd.  The name of this file is passed as an argument to the
   current command as the result of the expansion.  If the >(list)
   form is used, writing to the file will provide input for list.
   If the <(list) form is used, the file passed as an argument
   should be read to obtain the output of list.

   When available, process substitution is performed
   simultaneously with parameter and variable expansion,
   command substitution, and arithmetic expansion.
5

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .