Arun
Arun

Reputation: 1220

Compare two files and get the matching line result from 2nd file

I have two files ,

$cat file_1.txt
95335df46cfdb345c0214296e0043c00,NA
a0af947a85e6895dab70eaec136cfed2,NA

$cat file_2.txt
77f673137c17b4b0405d13060e9715a3,5,X,Y
874d51610c15975c82c081aba0b096c3,5,A,M
95335df46cfdb345c0214296e0043c00,5,M,N

I'm comparing first field from "file_1.txt" against first field against "file_2.txt". If there is 'hash' any match , get the complete matching line from "file_2.txt".

Matched Line : ( From file_2.txt )

95335df46cfdb345c0214296e0043c00,5,M,N

I tried using awk, But not getting any results.

$ awk 'NR==FNR{a[$1];next} ($1) in a' file_1.txt file_2.txt

Am i making any mistake here ? Any suggestions please ?

Upvotes: 0

Views: 44

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Using join command:

join -t',' -o2.1,2.2,2.3,2.4 <(sort file_1.txt) <(sort file_2.txt)

The output:

95335df46cfdb345c0214296e0043c00,5,M,N

  • -t',' - input/output field separator

Upvotes: 1

Sundeep
Sundeep

Reputation: 23667

$ awk -F, 'NR==FNR{a[$1];next} $1 in a' file_1.txt file_2.txt
95335df46cfdb345c0214296e0043c00,5,M,N

You missed specifying that , is delimiter to use

Upvotes: 2

Related Questions