2

I have two files:

file1:

905894
1197693
3703749

file2:

905894   Test1
1197693   Test2
3703749  Test3

I want to get the values in the second column if elements in file 1 column 1 matches any of the words in file 2 column 1.

I know awk can do it, but stuck in reading file 1 and using a loop.

heemayl
  • 56,300
  • You can look at this for reference - https://unix.stackexchange.com/questions/28158/is-there-a-tool-to-get-the-lines-in-one-file-that-are-not-in-another – rahul Apr 30 '15 at 20:15
  • What you need here is either join -o 2.2 <(sort file1) <(sort file2) or awk 'NR==FNR{a[$1]; next} ($1 in a) {print $2}' file1 file2 – don_crissti Apr 30 '15 at 22:33

2 Answers2

2

Using grep:

$ grep -wFf file1 file2 
905894   Test1
1197693   Test2
3703749  Test3
  • -f indicates that we are taking patterns (one per line) to be matched from file1

  • -F will take the patterns from file1 as fixed strings meaning no further operations with be done on them prior to matching

  • -w indicates we are matching lines that contains the patterns as whole words. Note that only [A-Za-z0-9_] are considered as word constituent characters, any other character will be considered as word delimiter. This will ensure that only whole columns are matched, not anything like 905894abcd in the columns.

Check man grep to get more idea on this.

heemayl
  • 56,300
0

I'd use perl myself.

  • read in file 1, extract a list of numbers.
  • iterate file 2, extracting number and word
  • check if 'number' in file2 was present in file1, and if so print the word.

Something like this:

#!/usr/bin/perl

use strict;
use warnings;

my %in_file1;

open( my $file1, "<", "file1.txt" ) or die $!;

while (<$file1>) {
    my ($number) = m/(\d+)/;
    $in_file1{$number}++;
}
close($file1);

open( my $file2, "<", "file2.txt" ) or die $!;

while (<$file2>) {
    my ( $number, $word ) = split;
    if ( $in_file1{$number} ) { print $word, "\n"; }
}

close($file2);
Sobrique
  • 4,424