Search code examples
awkgrepmatchingcarriage-return

awk printed out only one matching line when multiple terms in the first column in file1 matched to the first column in file2


I know similar questions have been asked multiple times, but I was not able to get the code to work with my files. I have one column in my file 1 and multiple columns in my file 2. What I wanted to do is exactly like what the person asked in this post, which is if column 1 in file 1 matches column 1 in file 2, print out the entire line in file 2. https://unix.stackexchange.com/questions/334448/if-column-matches-another-file-print-every-line-with-match-awk-grep?noredirect=1&lq=1

I tried awk 'NR==FNR{c[$1]++;next};c[$1]>0' file1 file2 and also tried re-formatting my files following the code, but no success.

tr '\r' '\n' < file1 > file1.new
mv file1.new file1
tr '\r' '\n' < file2 > file2.new
mv file2.new file2

The problem I am having is that awk only prints out one of the matching lines (not the first matching line). I am sure there are multiple matching lines because I checked it with grep.

I was wondering if anyone knows the potential cause of it. Thank you for your help!!!


Solution

  • Could you please try following.

    1st solution: Use removal of control M logic in awk itself.

    awk '{gsub(/\r/,"")} NR==FNR{c[$1]++;next};c[$1]>0' file1
    

    2nd solution: Remove them by tr and then use awk(trying to fix OP's attempt itself).

    tr -d '\r' < file1 > file1.new
    tr -d '\r' < file2 > file2.new