Matching data in separate files

Viewed 101

I've seem several questions of this type and have truly tried to work this out on my own without success.

I have two files of data; I want to compare their first fields and print the line from the second file next to the matching entry from the first file in an output file. It is important to maintain the list order in file 1, including the lines with no matches. The files are tab separated values but I can change them if required (although commas are in use in the data).

file1.txt

37600
39219
32887
01262
69241
00361
34180
42385
69245

file2.txt

37600       GEAR PUMP   1
32887       MOTOR, 480V 1
34180       BRACKET 2
00361       WASHER  4
42385       SCREW       8

Desired Output file

37600       GEAR PUMP   1
39219
32887       MOTOR, 480V 1
01262
69241
00361       WASHER  4
34180       BRACKET 2
42385       SCREW       8
69245

This is my best effort so far but it results in an empty file.

awk NR==NFR{a[$1];next}$1 in a {print $0} file1.txt file2.txt > outfile.txt

Any help would be greatly appreciated.

2 Answers
Related