1

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.

2
1

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.

2
  • Perhaps adding --word-regexp would be even better? – dhag Apr 30 '15 at 21:17
  • @dhag: would be better in this case..added – heemayl Apr 30 '15 at 21:27
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);

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