Search code examples
bashawkgrepcomm

Comparing output from two greps


I have two C source files with lots of defines and I want to compare them to each other and filter out lines that do not match. The grep (grep NO_BCM_ include/soc/mcm/allenum.h | grep -v 56440) output of the first file may look like:

...
...
# if !defined(NO_BCM_5675_A0)
# if !defined(NO_BCM_88660_A0)
# if !defined(NO_BCM_2801PM_A0)
...
...

where grep (grep "define NO_BCM" include/sdk_custom_config.h) of the second looks like:

...
...
#define NO_BCM_56260_B0
#define NO_BCM_5675_A0
#define NO_BCM_56160_A0
...
...

So now I want to find any type number in the braces above that are missing from the #define below. How do I best go about this? Thank you


Solution

  • You could use an awk logic with two process-substitution handlers for grep

    awk 'FNR==NR{seen[$2]; next}!($2 in seen)' FS=" " <(grep "define NO_BCM" include/sdk_custom_config.h) FS="[()]" <(grep NO_BCM_ include/soc/mcm/allenum.h | grep -v 56440)
    # if !defined(NO_BCM_88660_A0)
    # if !defined(NO_BCM_2801PM_A0)
    

    The idea is the commands within <() will execute and produce the output as needed. The usage of FS before the outputs are to ensure the common entity is parsed with a proper-delimiter.

    FS="[()]" is to capture $2 as the unique field in second-group and FS=" " for the default whitespace de-limiting on first group.

    The core logic of awk is identifying not repeating elements, i.e. FNR==NR parses the first group storing the unique entries in $2 as a hash-map. Once all the lines are parsed, !($2 in seen) is executed on the second-group which means filter those lines whose $2 from second-group is not present in the hash created.