Dom
Dom

Reputation: 17

grep - how could I search a file for numbers from second file

I have two lists of numbers

file1 has names and numbers

alaska5554443333
california5556667777
hawaii5555559999

file2 only has numbers, but only the first 6

555333
555999
555222

How could I search file1 and take out any matches from file2 while only looking at the first 6 numbers so that I don't accidentally take out a match from the middle or end of the number?

When I had full numbers I went through the file using grep -v -f file2 file1 > file3 so that I was pulling out all the names and numbers from file1 that didn't have a match in file2.

Upvotes: 0

Views: 82

Answers (2)

Ariel
Ariel

Reputation: 26783

You will need to pre-process file2.

sed 's/^/^[^0-9]+/' < file2 | egrep -v -f - file1

This adds a regular expression before each number in file2. The expression looks for a string of non-digits, followed by the number from each line of file2.

egrep then uses those patterns to exclude lines from file1.

Upvotes: 1

David
David

Reputation: 6561

I would use for this:

open NAMESANDNUMS, "<namesandnums.txt" or die $!;
my @namesandnums = <NAMESANDNUMS>;
close NAMESANDNUMS or die $!;

open NUMBERS, "<numbers.txt" or die $!;
my @numbers = <NUMBERS>;
close NUMBERS or die $!;

foreach(@namesandnums) {
    if(/[a-z]+(\d{6})/) {
        my $cnum = $1;
        foreach(@numbers) {
            print $_ if($_ == $cnum);
        }
    }
}

Upvotes: 0

Related Questions