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 protected]:phoneexample

username.email.txt - contains:

user1:[email protected]
user131:[email protected]

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
...

1 Answer 1

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
1
  • Thanks I'll give it a shot :) and accept answer once tested. Commented Mar 28, 2019 at 23:22

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.