If column1 of file1 is present in column1 file2, then I want to print whole row of file1. This was solved in a previous post, so I was able to get my code to work, but couldn't figure out why my previous solution didn't work. File1 is tab delimited.
File 1:
10001 stuff1,stuff2
20002 stuff3,stuff4
30003 stuff5
File 2:
10001
30003
Output:
10001 stuff1,stuff2
30003 stuff5
This is what worked, but I don't understand what gsub is or why we only print if greater than 0:
awk '{gsub(/\r/,"")} NR==FNR{c[$1]++;next};c[$1]>0' file2 file1
I tried this by modifying some other folks' codes, but I want to know what was wrong with it.
nawk -F, 'BEGIN{FS=OFS='\t'} FNR==NR{array[$1]; next;} ($1 in array){print$0}' file2 file1
The code that didn't work prints both lines of file1. All of file1 is printed, instead of making sure that the lines are present in file 2 first. Any idea what I did wrong?