Search code examples
awkcarriage-return

awk code to output lines with matching fields, but no output


I'm trying to output lines in one file fileB that are matched with parameters in another file fileA, but my attempt doesn't output anything.

fileA:

chr1    7237    7238
chr1    7637    7637
chr1    7908    7908
chr1    8045    8045
chr1    8329    8329
chr1    8358    8358
chr1    8375    8375
chr1    8381    8381

fileB: (note that the fifth line has an extra column)

130 chr1    7237    7238    0k9imgkt
135 chr1    7637    7637    b9gko
138 chr1    7908    7908    kob9g
139 chr1    8045    8045    34e5rg  4r
151 chr1    8329    8329    b
151 chr1    8346    8346    345y46htyh
151 chr1    8346    8346    76jtuj
152 chr1    8358    8358    asfge

My code:

awk 'NR==FNR{a[$1,$2,$3];next}($2,$3,$4) in a{print $0}' fileA fileB

Expected output:

130 chr1    7237    7238    0k9imgkt
135 chr1    7637    7637    b9gko
138 chr1    7908    7908    kob9g
139 chr1    8045    8045    34e5rg  4r
151 chr1    8329    8329    b
152 chr1    8358    8358    asfge

Actual output:



Solution

  • This happens when input files have Windows line endings (i.e \r\n). Fix your input files using dos2unix tool and your command will work.

    dos2unix fileA fileB
    

    Or if you're using gawk or mawk, set record separator to \r\n:

    awk -v RS='\r\n' ...