1

How could I loop through each file in a directory using this awk command?

awk -F: 'FNR==NR{a[$1]=$2;next} $2 in a{print $1 FS a[$2]}' email.phone.txt username.email.txt

Basically this partially matches columns and outputs the results in a combined fashion.

Example of this would be >

email.phone.txt - contains:

email@email.com:phoneexample

username.email.txt - contains:

user1:email@email.com
user131:email@email.com

EXPECTED OUTPUT - should be:

user1:phoneexample
user131:phoneexample

but this only works on 1 text file being

email.phone.txt

however I have multiple files with the same content in a subdirectory, example:

../lists/
email.phone1.txt
email.phone2.txt
email.phone3.txt
....

and it would be quite time consuming to individually awk each file, is it possible I could use a for loop for each file in directory and output example >

result1.txt
result2.txt
result3.txt
...
0

Do you mean to run that awk script for each email.phone*.txt file, with the same username.email.txt? Something like this should do. The tail part (1.txt, etc.) of the output filename is taken from the input filename.

for f in email.phone*.txt; do
    out="result${f#email.phone}"       # remove leading 'email.phone'
    awk -F: 'FNR==NR{a[$1]=$2;next} $2 in a{print $1 FS a[$2]}' \
        "$f" username.email.txt > "$out"
done
  • Thanks I'll give it a shot :) and accept answer once tested. – user343683 Mar 28 at 23:22

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.