Perl Regex Help - find lines in another file -
i want match occur it's not happening.
i have issue. wish find occurences of lines 1 file in another.
here's 1 file (@file)
735 1 1 1891 1 0 2021 1 1 1892 2 1 667 1 0 802 2 1 665 1 0 666 1 1 596 1 0 3193 2 1
here's 1 in have find above lines (@file1)
1521 1 0 : 1167 0 0 : 1167 2 0 : 1167 1 0 ; 2605 1 1 ; 2280 0 1 : 2280 2 0 : 1892 0 0 : 2280 1 0 : 2021 0 0 ; 1892 2 1 : 667 0 1 : 667 1 0 ; 1892 1 1 ;
here's code wrote
foreach $leadline (@file1) { foreach $line (@file) { $_ = ' ' . $leadline; $line = ' ' . $line; if (m/$line/) { push @final, $_; } } }
but unable detect lines.
@file1
, @file
variables store contents of files.
i either no lines detected or lines detected.
the reason concatenating space before 2 lines , 667 1 0 can occur first phrase in given line.i not comfortable regex in regex directly.
note :- if line , line j in first file occur pattern in same line of other file output should 1 of lines. , if pattern 1667 1 0 found, shouldn't confused 667 1 0. hence added whitespace.
i able achieve goal in python unable replicate in perl . here's python snippet :-
for line1 in file1: j in range(0,len(file0)-1):# in file0: if ' '+lines[j][0:len(file0[j])-1] in ' '+line1: = + 1 print line1[0:len(line1)-1] break
expected output :- 1892 2 1 : 667 0 1 : 667 1 0 ;
i think solution different problem, here anyway!
use warnings; use strict; use 5.010; use array::utils 'array_diff'; open $fh, '<', 'f1.txt' or die $!; @f1; while ( <$fh> ) { push @f1, [split]; } @final; open $fh, '<', 'f2.txt' or die $!; while ( <$fh> ) { @f2 = map [ /\d+/g ], split /:/; $f1 ( @f1 ) { @matches = grep { not array_diff(@$f1, @$_) } @f2; push @final, map "@$_", @matches; } } @final;
output
1892 2 1 667 0 1 667 1 0
update
okay here's second attempt! choroba
wrote using map
, addition of stripping trailing whitespace on data first file.
use warnings; use strict; use 5.014; # non-destructive substitution open $fh, '<', 'f1.txt' or die $!; @f1 = map s/\s+\z//r, <$fh>; $re = join '|', @f1; open $fh, '<', 'f2.txt' or die $!; @final = grep /\b(?:$re)\b/, <$fh>; print @final;
output
1892 2 1 : 667 0 1 : 667 1 0 ;
Comments
Post a Comment