Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

Compare file1 and file2, print matching lines in same order as file1

$
0
0
I want to print only the lines in file2 that match file1, in the same order as they appear in file 1

file1
Quote:

c2
c23
c14
c22
file2
Quote:

c14 CGAGGCTG
c2 CAGAGAGG
c20 CTCGACTC
c22 CGAGGCTG
c23 AAGAGGCA
desired output:
Quote:

c2 CAGAGAGG
c23 AAGAGGCA
c14 CGAGGCTG
c22 CGAGGCTG
I'm getting the lines to match
Code:

awk 'FNR==NR {a[$1]++}; FNR!=NR && a[$1]' file1 file2
but they are in sorted order, which is not what I want:
Quote:

c14 CGAGGCTG
c2 CAGAGAGG
c22 CGAGGCTG
c23 AAGAGGCA
Can anyone help me out?

Viewing all articles
Browse latest Browse all 16232

Trending Articles