Awk - Count instances of a number in col1 and put results in a col2 (new) of diff file

I have 2 files as follows:

filename1: [FileID FileName FileSize]:

6742 /welcome/mundial98_ahf1_404.htm 1020
6743 /welcome/mundial98_ahf1_404.htm 2224
6744 /welcome/mundial_ef1_404.htm 21678
6745 /welcome/mundial_if_404.htm 4236
6746 /welcome/mundial_lf1_404.htm 21678

filename2: [FileID timestamp operation FileName FileSize]

6746 894694763 1 /welcome/mundial_lf1_404.htm 21678
6742 894694763 1 /welcome/mundial98_ahf1_404.htm 1020
6742 894694763 1 /welcome/mundial98_ahf1_404.htm 1020
6745 894694763 1 /welcome/mundial_if_404.htm 4236
6746 894694763 1 /welcome/mundial_lf1_404.htm 21678
6746 894694763 1 /welcome/mundial_lf1_404.htm 21678

I need to count the occurrences of each number in col1 of filename2 and place the them in a new col (between col1 and col2) of filename1.They should be in the row corresponding to the 1st column of each file (FileID).

So for the 2 files above, the expected output should be a new filename1 with a new column (NumRequests):
filename1 (new): [FileID NumRequests FileName FileSize]:

6742 2 /welcome/mundial98_ahf1_404.htm 1020
6743 0 /welcome/mundial98_ahf1_404.htm 2224
6744 0 /welcome/mundial_ef1_404.htm 21678
6745 1 /welcome/mundial_if_404.htm  4236
6746 3 /welcome/mundial_lf1_404.htm 21678

Thank you so much for your help!

Try:

awk 'NR==FNR{a[$1]++;next}
{$1=a[$1]?$1 FS a[$1]:$1 FS "0"}
1' filename2 filename1  > newfile
1 Like

This worked perfectly! Thank you!