1

I have two pipe delimited file like below

file1.txt

A1234|JESSIE|OPTED
A1224|JOHN|OPTED
L1212|RAMSAY|OPTED
L1832|TIZEN|TESTED

file2.txt

A1234|B1465
G1211|L1211
G1241|L1212
G1271|L1232

Desired output

A1234|B1465
G1241|L1212

I am trying to compare column 1 and column 2 in file2.txt with column 1 in file1.txt and get the matching rows in file2.txt if it the first column in file1.txt matches with either column 1 or column 2 in file2.txt.

I tried the awk below but it doesn't appear to be giving me the right results.

awk -F'|' 'FNR==NR{a[$1]=1; next} a[$1,2]' file1.txt file2.txt > output.txt

1 Answer 1

3
$ awk -F '|' 'NR==FNR{a[$1]; next} ($1 in a) || ($2 in a)' file1.txt file2.txt
A1234|B1465
G1241|L1212

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.