AmyC
AmyC

Reputation: 9

How to use awk shell script to compare and match two files

Basically, I have two files

dupestest.txt

152,153
192,193
215,216
290,291
2279,2280
2282,2283

haftest.txt

152,ABBOTS ROAD
153,ABBOTS ROAD
154,ABBOTS ROAD
155,ABBOTS ROAD
156,ABBOTS ROAD
157,ABBOTS ROAD

I want to find the numbers in dupestest.txt in haftest.txt and produce this outcome: results.txt

152,ABBOTS ROAD,153 ABBOTS ROAD 
192,ABBOTS ROAD,193,ABBOTS ROAD

etc

Can anyone give me any advice?

It has to be in awk.

Upvotes: 0

Views: 496

Answers (2)

Kent
Kent

Reputation: 195229

try this

awk -F, 'NR==FNR{a[$1]=$0;next}$1 in a&&$2 in a{print a[$1]","a[$2]}' haft.txt dup.txt

Upvotes: 0

neevek
neevek

Reputation: 12148

Try this:

awk -F, 'BEGIN{OFS=","} FNR==NR{a[$1]=$2; next} $1 in a || $2 in a{print $1, a[$1], $2, a[$2]}' haftest.txt dupestest.txt

This line of script tests if either the first or the second key in dupestest.txt exists in haftest.txt, and prints its associated value in dupestest.txt if one of the keys exists, you may have to tweak the script a little bit if only one of the two keys exists in haftest.txt to get the desired output, that's left as an exercise for you.

Upvotes: 3

Related Questions