awk exact variable match, print remaining lines

109 Views Asked by At

I have two files, If FILE2's column2 data(/usr, /opt/var) available in FILE1's column2, then print the remaining lines of FILE1.

The line order will vary, and the columns content also vary from server to server.

FILE1

JUDI /usr 94  
JUDI /var 78  
JUDI /usr/openv 80  
JUDI /opt 85  
JUDI /opt/var 75  

FILE2

JUDI /usr 93  
JUDI /opt/var 70  

OUTPUT as

JUDI /var 78  
JUDI /usr/openv 80  
JUDI /opt 85 

I tried this code:

A=`awk '{print $2}' FILE2`
for i in $A
do
x=$i
nawk '$2 !~ /^"'\$\x'"/  {print $0}' FILE1
done
1

There are 1 best solutions below

1
ghoti On BEST ANSWER
$ awk 'NR==FNR{a[$2]++} !($2 in a)' file2 file1
JUDI /var 78
JUDI /usr/openv 80
JUDI /opt 85

Tested in FreeBSD, but should work with just about every awk implementation.

The idea here is that you step through the first file, populating an array with the second field as a key, then step through the second file, printing only the lines whose second field are not a key in the array.