如何从文件中 grep 单词



我想从另一个文件中对一个文件进行一些单词的grep。我的代码能够 grep 文件最后一行的单词,但不能处理它前面的单词。我不知道为什么,希望能在这里得到帮助。下面是我使用的perl脚本:

open(FILE1,"file1.txt") or die "Error, File1 could not openn";           
open(FILE2,"file2.txt") or die "Error, File2 could not openn";
open(FILE3, ">file3.txt") or die "Error, File3 could not openn";
use strict; 
use warnings;
use List::MoreUtils qw(uniq);
my @file1=<FILE1>;
my @file2=<FILE2>;
my $j =0;
my $i =0;
my $zone =0;
for ($j=0; $j<=$#file2; $j++){
    $zone = $file2[$j];
    unless ( $zone =~ m/#(.*?)/ ) {
        print "$zone";
        my @fid = grep /$zone/ , @file1;
        @fid = uniq(@fid);
        s{^s+|s+$}{}g foreach @fid;                #cancel leading space
        for ($i=0; $i<=$#fid; $i++){
            print FILE3 "$fid[$i]n";
        }
        #@fid=();
    }
}
close(FILE3);

我的文件1.txt是这样的:

i am a dog
i am a cat
we are the fish
he is a boy
she is a girl

我的文件2.txt是这样的:

is
am

但是我的 file3 只能显示那些句子包含 am 但没有是,如果我把 is 放在第二行并且 am 在第一行,那么我的 file3 只包含带有 is 的句子。我不太确定为什么我的代码只能 grep 文件中的最后一行2。感谢您的帮助。

从文件读取时,最后一个换行符是读取的每一行的一部分。您可以通过嘀啾从模式数组中删除换行符:

chomp( my @file2 = <FILE2> );

你已经可以用 egrep 做到这一点了:

egrep -f file2.txt file1.txt

这个问题的根源是chomp - 您没有删除换行符,因此匹配不起作用。

但除此之外,您的代码还有一些问题可以解决:

  • 打开文件,您应该使用 3 个带有词法文件句柄的 arg open,因为它的样式更好:open (my $file1, '<', 'file1.txt' ) or die $!;
  • 与其循环循环,不如编译一个"匹配正则表达式"。
  • 您可以逐行迭代,而不是将所有文件读入数组,并且不需要使用内存。
  • 如果要迭代循环,并且使用索引来访问当前元素,则最好使用foreach my $line ( @things ) {类型语法。

因此,您的代码实际上可以简化为:

#!/usr/bin/env perl
use strict;
use warnings;
open(my $data, '<',"file1.txt") or die $!;
open(my $search, '<', "file2.txt") or die $!;
open(my $output, '>', "file3.txt" ) or die $!;
chomp ( my @search_terms = <$search> );
#quotemeta is needed to avoid 'special' regex characters doing things. 
my $search_regex = join "|", map { quotemeta }, @search_terms;
#note - 'b' denotes word boundary, which may not be what you want.  
#means 'is' won't match 'fish'
#so get rid of them if that's not what you want. 
$search_regex = qr/b($search_regex)b/;
print "Using: $search_regexn";
select $output; #default print destination
while ( <$data> ) {
    print if m/$search_regex/;
}

输出(在"file3.txt"中(:

i am a dog
i am a cat
he is a boy
she is a girl
<</div> div class="one_answers">

请尝试这个。

use strict; 
use warnings;
use List::MoreUtils qw(uniq);

open(FILE1,"file1.txt") or die "Error, File1 could not openn";           
open(FILE2,"file2.txt") or die "Error, File2 could not openn"; 
open(FILE3, ">file3.txt") or die "Error, File3 could not openn"; 
my @file1=<FILE1>;
my @file2=<FILE2>;
my $j =0;
my $i =0;
foreach my $main_line(@file1){
    chomp($main_line);
    foreach my $line(@file2){
        chomp($line);
        if ($main_line =~ /$line/i) {
            print FILE3 "$main_linen";
        }
    }
}
close(FILE3);

谢谢普拉文兹x~

最新更新