I'm currently using

 $ awk 'NR==FNR{a[$1];next} ($3 in a)' find.txt path_to_100_files/*

which uses find.txt strings to search multiple files within path_to_100_files/ for matches.

find.txt contains


and then path_to_100_files/ contains files such as

now what this is doing is only searching the 3rd column for the strings from find.txt, but i need it to search the entire file/every column?

as some files may be 5 columns long, or 9 columns long example,

I've tried to change ($3 in a) to like ($0-$9 in a) but doesn't seem to work?


Why search fields one-by-one? Why not search whole lines at a time?

grep -f find.txt path_to_100_files/*
  • You should add a -F as well for fixed-string search.
    – Wildcard
    Jan 6 '18 at 1:04
  • @Wildcard As well as add a -o to only output matches.
    – ImHere
    Jan 6 '18 at 2:42

If the email is the last field in the 100 files, use:

awk -F: 'NR==FNR{a[$1];next} ($NF in a)' find.txt path_to_100_files/*

If you must look into each field, you need a loop:

awk -F: 'NR==FNR{a[$1];next} {for (i=1;i<=NF;i++) {if ($i in a) {print;break}}} find.txt path_to_100_files/*

But the simpler call is using grep:

grep -oFf find.txt path_to_100_files/*

First find the maximum column exsists in the files using below command and take the maximum value from it

k=`awk -F ":" '{print NF}'  path_to_100_files/*  | sort -nr | head -1`

Used for loop to get it checked with every column

for ((i=0;i<=$k;i++)); do awk -v i="$i" -F ":" 'NR==FNR {a[$1];next}($i in a) {print }’   find.txt path_to_100_files/*   ; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.