如何使用perl从一个文件中读取字符串并检查该字符串是否在另一个文件中

如何使用perl从一个文件中读取字符串并检查该字符串是否在另一个文件中,perl,Perl,我有两个文本文件。一个包含一些数据的文件,如 hi how 还有另一个像这样的文件 L_ hello hi whats up N_ david N_ jhon N_ abraham N_ mc D L_ hey how u doing N_ david N_ jhon N_ abraham N_ mc D L_ some blah blah blah N_ david N_ jhon N_ abraham N_ mc D 如何从第一个文件中提取一行并检查另一

我有两个文本文件。一个包含一些数据的文件,如

 hi 
 how
还有另一个像这样的文件

L_ hello hi whats up
 N_ david
 N_ jhon
 N_ abraham
 N_ mc D  
L_ hey how u doing
 N_ david
 N_ jhon
 N_ abraham
 N_ mc D
L_ some blah blah blah
 N_ david
 N_ jhon
 N_ abraham
 N_ mc D
如何从第一个文件中提取一行并检查另一个文件中是否存在该行

如果该行存在(例如,在我的示例中为
hi
),我只需要打印该搜索字符串和该行下方的名称。考虑<代码> Ly是用于检查字符串的标识符,如果字符串存在于该行中,我希望用标识符<代码> N> < /C>而在该行下方打印名称,而不是打印其他标识符,同时打印标识符<代码> Ly和<代码> N< < /C> >。 我正在寻找有关如何使用Perl解决此问题的建议。

尝试以下方法:

perl -ne'
  BEGIN{
  $x = pop;
  ($re) = map qr/$_/, join "|", map /(\w+)/ && qr/\Q$1/, <>;
  @ARGV = $x;
  }
  $b = /($re)/ and print("\n$1"),next if /^L_/;
  chomp; s/^\w+_//;
  print if $b
' file1 file2
#!/usr/bin/perl

use strict;
use warnings;

open my $patterns, '<', "$ARGV[0]"
    or die "Cannot open $ARGV[0]:$!";
open my $data, '<', "$ARGV[1]"
    or die "Cannot open $ARGV[1]:$!";

my @patterns;
while (<$patterns>) {
    chomp;
    push @patterns, $_;
}

my $line;
LINE: while ($line = <$data>) {
    chomp $line;
    if ($line =~ m/^\s*L_/) {
        foreach my $pat (@patterns) {
            if ($line =~ m/$pat/) {
                print "$line\n";
                while ($line = <$data>) {
                    if ($line =~ m/^\s*N_/) {
                        print $line;
                    }
                    else {
                        redo LINE;
                    }
                }
            }
        }
    }
}
#/usr/bin/perl
严格使用;
使用警告;

打开我的$patterns,“您可能想看看perl函数open和grep

    my $datafile = "some_data_file.txt";
    my $comparefile = "data_to_compare.txt";
    open ( my $data_filehandle, "<", $datafile ) or die $!;
    my @things_to_find = <$data_filehandle>;

    open ( my $compare_filehandle, "<", $comparefile ) or die $!;
    while ( my $line = <$compare_filehandle> ) 
    {
       foreach my $thing ( @things_to_find )
       {
          print "Match found with: $line" if $line =~ m/$thing/;
          print "Next line is :", <$compare_filehandle>;
       }
    }
my$datafile=“some\u data\u file.txt”;
my$comparefile=“data\u to\u compare.txt”;

打开(my$data_filehandle),“请慢慢改进您的问题(减少示例数据大小,改进拼写等)。此外,为什么您需要使用perl来完成这项工作?@MichaWiedenmann我给出了一个与我的问题类似的小示例。我的数据看起来更糟,这就是为什么我选择了这个示例。我希望输出结果与hi David jhon Abraham mc D How David jhon Abraham一样如果你能在第二个文件中找到一个字符串,那么该文件下的所有名称都应该是printed@user3388427那么你应该修正你问题中的描述。谢谢你,李,你所做的很好,但是你能删除这些标识符之前的N和L吗strings@user3388427更改
打印“$line\n“
打印(拆分/,$line,2)[1],”\n“
,以及另一个类似的
打印()。
    my $datafile = "some_data_file.txt";
    my $comparefile = "data_to_compare.txt";
    open ( my $data_filehandle, "<", $datafile ) or die $!;
    my @things_to_find = <$data_filehandle>;

    open ( my $compare_filehandle, "<", $comparefile ) or die $!;
    while ( my $line = <$compare_filehandle> ) 
    {
       foreach my $thing ( @things_to_find )
       {
          print "Match found with: $line" if $line =~ m/$thing/;
          print "Next line is :", <$compare_filehandle>;
       }
    }