0

I have two files

file1:

45 789 
46 4546
78 4564
71 546

file2:

41 456 
46 456
40 465
45 456

I would like to compare these files and get an output: if the number in first column from first file is in the first column of the second file print a row from second file that include this number.

Desired output:

46 456
45 456

It will be something with grep? Thank you very much

2

Read the first file's column to populate an array of "seen" values. Then when reading the second file, if the first value has been "seen", print the record:

$ awk 'NR == FNR { data[$1] = 1 } NR != FNR && data[$1] { print }' file1 file2
46 456
45 456

Rather than using the FR == NR and FR != NR tests, we could also use FILENAME == "file1" and FILENAME == "file2".

  • Thank you very much and what is the difference of these solutions? – Lukáš Altman Apr 23 at 19:53
  • The difference is that the solution given will work on any number of subsequent files beyond the first two; hard-coding the file names will only work on two files with the specific names given. Comparing NR and FNR is a simple "is this the first file or literally any other file" test. – DopeGhoti Apr 23 at 19:56
  • Thank you for your help – Lukáš Altman Apr 24 at 2:40
1

Assuming your shell is bash/ksh/zsh:

join -o 0,2.2 <(sort file1) <(sort file2)
1

command

awk 'NR==FNR{a[$1];next}($1 in a){print $0}' file1 file2

output

46 456
45 456
  • This code-only answer would be much improved if you explained what and how the awk code is doing. – Anthony Geoghegan Apr 24 at 10:53
  • This works much like the currently accepted answer; the main difference is that it "short circuits" after filling the "seen values" array rather than testing for inequality of NR and FNR. – DopeGhoti Apr 24 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.