Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] bash and grep and diff



On Tue, 10 Aug 2010 20:28:14 +0900
Darren Cook <darren@example.com> wrote:

> I often want to run diff on just a subset of two files. I.e.
>   grep "ABC" a.txt >tmp1.txt
>   grep "ABC" b.txt >tmp2.txt
>   diff tmp1.txt tmp2.txt
> 
> Is there a way to do that in a single line, without having to create
> temp files?  (From a bash commandline, if at all possible.)  (There is
> only one stdin pipe, so I'm guessing not, but thought it wouldn't hurt
> to ask...)

You can do it by using the sub-shell feature of bash:

diff <(grep blah a.txt) <(grep blah b.txt)

the <() runs a sub-shell and uses /dev/fdX as input file for the
diff command. This needs some kernel feature, which is safe to 
assume as present and pipe compatible input file handling
(ie no seeking done on the files).

You can use even complex sub-shell comands with pipes and everything.
What i often do is something of the kind:

diff <( grep blah a.txt | sed -e /blub// | sort)  <( grep blah b.txt | sed -e /blub// | sort)

Please note that <() is a bashism and doesnt necessarily work on
other shells.

			Attila Kinali
-- 
If you want to walk fast, walk alone.
If you want to walk far, walk together.
		-- African proverb


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links