Oleguer Carreras Oleguer Carreras - 3 months ago 18
Linux Question

AWK matching fields between two files

I'm new with sh and AWK comand, I've been trying some commands with different options but didn't work :(

I've two files and i want to match the position 22,3 from the file1 with the string on the file2. If there is no match, I need to write the file1 line.

File1:


/batch/shared/TZ/TMP/AAA

/batch/shared/TZ/TMP/BBB

/batch/shared/TZ/TMP/CCC

/batch/shared/TZ/TMP/DDD


File2:


BBB

DDD


Result:


/batch/shared/TZ/TMP/BBB

/batch/shared/TZ/TMP/DDD


I never wrote on this page but saved my ass a lot of times.
BIG THANKS!

Answer

This is exactly you are looking for :

awk 'NR==FNR{a[$0]=1;next} {n=0;for(i in a){if(substr($0,22,3)~i) { n=1}  } } n' file2 file1

Hope this helps :)