Anyway, let me start off by posting example code:
here is some sample data:use warnings; use strict; my $f1 = 'file1.txt'; my $f2 = 'file2.txt'; my @original; my @gottem; open( my $file1, '<', $f1 ); while ( my $line1 = (<$file1>) ) { next if ( $line1 =~ /^#/ ); chomp($line1); open( my $file2, '<', $f2 ); while ( my $line2 = (<$file2>) ) { next if ( $line2 =~ /^#/ ); chomp($line2); my ( $leftside, $rightside ) = split( /\s/, $line2, 2 ); if ( $line1 =~ $leftside ) { push( @original, $rightside ); } } } # print "$_\n" for(@original); for (@original) { open( my $file_2, '<', $f2 ); while ( my $line_2 = (<$file_2>) ) { my ( $first, $last ) = split( /\s/, $line_2, 2 ); if ( $last =~ $_ ) { push( @gottem, $line_2 ); } } } print $_ for (@gottem);
file2.txt123 456 789
Output:123 string 1 111 string 1 script should skip this line 222 string 1 333 string 1 456 string 2 444 string 2 it should skip this line as well 555 string 2 666 string 2 789 string 3 777 string 3 also skipping this line too 888 string 3 999 string 3
Thanks for the tips btw :)123 string 1 111 string 1 222 string 1 333 string 1 456 string 2 444 string 2 555 string 2 666 string 2 789 string 3 777 string 3 888 string 3 999 string 3
What I am trying to do is take each string from file1.txt, and match it in file2.txt which populates @original. Once that is done I loop through @original and compare it back against file2.txt but this time I am comparing the right side of the split to the current line in file2.txt Then a final push to @gottem, just incase I need to use this list somewhere else.
This will allow me to catch all lines that match with 'string 1' or 'string 2' in file2.txt, because those are what I am going for, but there is no way to get the whole string out of file2.txt without some kind of moderate algorithm.
Like I said, I really would love to see some other ways to do this.
If you have any questions on need me to try to clarify/explain it more, just let me know. EDIT: It seems there is indeed an error in the output, there are some additional 'string 2's in there after 'string 5'. I am not sure how they got in there but hopefully you still see my idea, and can expand the horizon :DEDIT: Updated the post with sample data and output is expected.
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |