1

I have two files:

smw:/working/iso_testing # cat a
QConvergeConsoleCLI-1.1.03-49.x86_64.rpm
aaa_base-13.2+git20140911.61c1681-1.3.i586.rpm
acpica-20140724-2.1.2.i586.rpm
test.rpm

AND

smw:/working/iso_testing # cat b
QConvergeConsoleCLI-1.1.03-49.x86_64.rpm
aaa_base-13.2+git20140911.61c1681-1.3.i586.rpm
acpica-20140724-2.1.2.i586.rpm

--If I diff them I see this:

smw:/working/iso_testing # diff a b
4d3
< test.rpm

I want to take THAT output (test.rpm) and grep it out of a third file. A.K.A. file c:

smw:/working/iso_testing # cat c
QConvergeConsoleCLI QConvergeConsoleCLI-1.1.03-49.x86_64.rpm
aaa_base aaa_base-13.2+git20140911.61c1681-1.3.i586.rpm
acpica acpica-20140724-2.1.2.i586.rpm
test test.rpm

My desired output would look something like

test test.rpm

Perhaps I have to many files to begin with but I'm stuck. Thanks in advance

File A is the list or rpm's from one repo, file B is a list of rpm's from a .csv file, and file C is a copy of file B but with package names, not just the rpm.***

1 Answer 1

0

Here is the TLDR solution:

for line in "$(diff a b | sed -n 's/^< //p')"; do grep "${line}" c; done

A more detailed explanation follows.


Probably the first thing you'll want to do is clean up the output of the diff command and extract the strings you want to search for.

Based on your example it looks like you want the lines that only occur in the first file (e.g. file "a").

The following command returns the lines from the first file and removes the "< " prefix:

diff <(diff a b | sed -n 's/^< //p')

Now you can loop over this cleaned-up diff and grep for each line, e.g.:

for line in "$(diff a b | sed -n 's/^< //p')"; do grep "${line}" c; done

This approach has the advantage of being pretty flexible, since you can replace the grep statement with an arbitrary command.

On the other hand, if you know that just want to search for those fixed strings then you can also use the -f flag for grep instead of using a for-loop:

grep -f <(diff a b | sed -n 's/^< //p') c

Another alternative is to invoke diff a second time:

diff <(diff a b | sed -n 's/^< //p') c

You can then process this output in a similar manner:

diff <(diff a b | sed -n 's/^< //p') c | sed -n 's/^< //p'
1
  • 1
    @roaima Thanks - good call. I always grep first and ask questions later.
    – igal
    Commented Oct 18, 2017 at 3:32

You must log in to answer this question.

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