Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

test.txt
Temp1 A code1 2 Temp2 B code1 2 Temp3 A code1 4 Temp3 B code1 4 Temp3 C code1 1 Temp3 C code1 4
test1.txt
Temp1 A code 2 Temp2 B code1 3 Temp3 C code1 1
I have a file test.txt and test1.txt
In test1.txt, "Temp1 A code 2" is common in test.txt and so it should be ignore
In the line,"Temp2 B code1 3" 2nd column is B,which is also present in test.tsv but 4th column is changed.
If the column 2 is same as column 2 of test.tsv and if the column 4 is different,
I have to write the output to file.
The output should be:
Temp2 B code1 2 Temp3 B code1 4 Temp3 C code1 1 Temp3 C code1 4

Replies are listed 'Best First'.
Re: Print the lines which matches with file2
by kennethk (Abbot) on Jul 13, 2010 at 17:49 UTC
    What have you tried? What didn't work? See How (Not) To Ask A Question. Without seeing code, we can't effectively help you.

    You can deal with the searching part of your problem fairly easily using a hash of hashes - see perlreftut and perllol.

    I attempted to follow your spec, but do not get your output from following your described algorithm. For example, according to the rule you used to eliminate Temp1 A code1 2, Temp3 C code1 1 should be eliminated as well. I've attached the code I wrote. As this clearly does not meet your spec, you can either revise the code yourself or clarify where my algorithm and yours differ.

    #!/usr/bin/perl use strict; use warnings; my %test1; open my $fh1, '<', 'test1.txt' or die "Open fail: $!"; while (<$fh1>) { my ($temp, $letter, undef, $number) = split; $test1{$temp}{$letter}{$number} = 1; } open my $fh, '<', 'test.txt' or die "Open fail: $!"; while (<$fh>) { my ($temp, $letter, undef, $number) = split; unless (defined $test1{$temp}{$letter}{$number}) { print; } }

    outputs

    Temp2 B code1 2 Temp3 A code1 4 Temp3 B code1 4 Temp3 C code1 4