Trying to use diff output to compare to a separate file

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
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.***

try awk - do a difference for a b in awk, remember it, find it c
Order file names parameters you give to awk is important: should be b then a then c where a is the file with the difference, c is the file to do a lookup,
will find many different records not in file b searching file c.

awk '
       FILENAME=="a" {arr[$0]++; next}
       FILENAME=="b" {if( !arr[[$0] ) {srch[$0]++}
       FILENAME=="c" {if(srch[$0]) {print FILENAME, $0}
      '  b a c   
1 Like

I'm pretty new to this, but am I doing something wrong?

smw:/working/iso_testing # awk '
>        FILENAME=="a" {arr[$0]++; next}
>        FILENAME=="b" {if( !arr[[$0] ) {srch[$0]++}
>        FILENAME=="c" {if(srch[$0]) {print FILENAME, $0}
>       '  b a c
awk: cmd. line:3:        FILENAME=="b" {if( !arr[[$0] ) {srch[$0]++}
awk: cmd. line:3:                                ^ syntax error
awk: cmd. line:4:        FILENAME=="c" {if(srch[$0]) {print FILENAME, $0}
awk: cmd. line:4:                      ^ syntax error
awk: cmd. line:5:
awk: cmd. line:5:       ^ unexpected newline or end of string

just a wild guess -extra [ for file the b case:

awk '
       FILENAME=="a" {arr[$0]++; next}
       FILENAME=="b" {if( !arr[$0] ) {srch[$0]++}
       FILENAME=="c" {if(srch[$0]) {print FILENAME, $0}
      '  b a c

if on Solaris, use nawk instead of awk .

1 Like

This for bash on SLES 12.

smw:/working/iso_testing # awk '
>        FILENAME=="a" {arr[$0]++; next}
>        FILENAME=="b" {if( !arr[$0] ) {srch[$0]++}
>        FILENAME=="c" {if(srch[$0]) {print FILENAME, $0}
>       '  b a c
awk: cmd. line:4:        FILENAME=="c" {if(srch[$0]) {print FILENAME, $0}
awk: cmd. line:4:                      ^ syntax error
awk: cmd. line:5:
awk: cmd. line:5:       ^ unexpected newline or end of string

ok, we're getting there...

awk '
       FILENAME=="a" {arr[$0]++; next}
       FILENAME=="b" {if( !arr[$0] ) srch[$0]++}
       FILENAME=="c" {if(srch[$0]) print FILENAME, $0}
      '  b a c

Hey!!!
No syntax errors :slight_smile:
No output either.
I was hoping to see:

test test.rpm

So I'll keep playing.

Unfortunately, there's a small logical flaw in above; the sequence of files and thus how the arrays are populated DOES matter. Try

awk '
       FILENAME=="b" {arr[$0]++; next}
       FILENAME=="a" {if( !arr[$0] ) srch[$0]++}
       FILENAME=="c" {if(srch[$2]) print}
      '  b a c
test test.rpm

EDIT: How about

grep $(diff -y -b --suppress-common-lines a b | cut -f1) c
test test.rpm
1 Like

Hi.

See also Trying to use diff output to compare to a separate file - Unix & Linux Stack Exchange

cheers, drl

1 Like

Thanks RudiC, your awk statement worked perfectly and I was able to test it out on some different scenarios.

I tried the same grep command you did, but got a little stuck when the output wasn't what I liked..

smw:/working/iso_testing # grep $(diff -y -b --suppress-common-lines a b | cut -f1) c
grep: zip.rpm: No such file or directory
c:test test.rpm

P.S. I added the zip zip.rpm to my FILE C and zip.rpm to file A.

Thank you much!!!!

That sounds like an unexpected result of the diff "command substitution", which in turn might be due to non-compliant input files' contents.
Please post the result of the diff alone / on its own.

smw:/working/iso_testing # diff -y -b --suppress-common-lines a b | cut -f1
test.rpm
zip.rpm
smw:/working/iso_testing #
smw:/working/iso_testing # grep $(diff -y -b --suppress-common-lines a b | cut -f1) c
grep: zip.rpm: No such file or directory
c:test test.rpm

Ah - sorry, my fault. Try enclosing the "command substitution" in double quotes and report back.