perl 如何打印10行以上,如果在一个文件中找到特定的字符串?

4uqofj5v  于 2022-11-15  发布在  Perl
关注(0)|答案(2)|浏览(181)

我想打印10行,包括特定的字符串行。我的代码只打印10行以上,它不能打印行,其中包括特定的字符串。请帮助建议以下是我的txt文件数据。

# 2022061804540300, Setting Wafer Attribute for 7021-24:ER0TM234SEF5:GOOD DIE COUNT to 329
# 2022061804540300, Setting Wafer Attribute for 7881-13:ER6BX271SEB6:GOOD DIE COUNT to 338
# 2022061804540300, Setting Wafer Attribute for 9791-03:WH77B083ESF4:GOOD DIE COUNT to 317
# 2022061804540300, Setting Wafer Attribute for 9791-10:WH77B189ESA5:GOOD DIE COUNT to 329
# 2022061804540300, Setting Wafer Attribute for 9791-12:WH77B195ESC7:GOOD DIE COUNT to 322
# 2022061804540300, Setting Lot Attribute GOOD DIE COUNT to 3585
# 2022061804540300, Setting Lot Attribute CURRENT QTY to 3585
# 2022061804540300, Setting Lot Attribute in Subcon section CURRENT QTY to 3585
# 2022061804540300, Updating DRF: /home/prbdata/tracking/prod/stage/dispo_results/2022061804535700_1864881_001_SLC_dispoinfo.xml
# 2022061804540300, No StartLot action for OFFLOAD - will not initiate dfs data transfer
# 2022061804540300, TRANSPORT_FILE = /home/prbdata/tracking/prod/transaction/outgoing/202206180400/2022061804535700_1864881_001_SLC_dispoinfo.xml
# 2022061804540300, cp: cannot create regular file ‘/mads/tpp/spool/EngineeringToolbox/dispoResults/2022061804535700_1864881_001_SLC_dispoinfo.xml’: Permission denied
# 2022061804540300, [EVENT::FATAL] 'PRBDATA_DFS_FAIL_COPY':'/home/prbdata/tracking/prod/transaction/outgoing/202206180400/2022061804535700_1864881_001_SLC_dispoinfo.xml':'/mads/tpp/spool/EngineeringToolbox/dispoResults'
# 2022061804540300, $VAR1 = bless( {
# 2022061804540300,                  '-line' => 413,

因此,如果在文件中找到“EVENT:FATAL”字符串,则将打印以上10行,包括“EVENT:FATAL”行。
我输出文件:

# 2022061804540300, Setting Wafer Attribute for 9791-10:WH77B189ESA5:GOOD DIE COUNT to 329
# 2022061804540300, Setting Wafer Attribute for 9791-12:WH77B195ESC7:GOOD DIE COUNT to 322
# 2022061804540300, Setting Lot Attribute GOOD DIE COUNT to 3585
# 2022061804540300, Setting Lot Attribute CURRENT QTY to 3585
# 2022061804540300, Setting Lot Attribute in Subcon section CURRENT QTY to 3585
# 2022061804540300, Updating DRF: /home/prbdata/tracking/prod/stage/dispo_results/2022061804535700_1864881_001_SLC_dispoinfo.xml
# 2022061804540300, No StartLot action for OFFLOAD - will not initiate dfs data transfer
# 2022061804540300, TRANSPORT_FILE = /home/prbdata/tracking/prod/transaction/outgoing/202206180400/2022061804535700_1864881_001_SLC_dispoinfo.xml
# 2022061804540300, cp: cannot create regular file ΓÇÿ/mads/tpp/spool/EngineeringToolbox/dispoResults/2022061804535700_1864881_001_SLC_dispoinfo.xmlΓÇÖ: Permission denied

预期文件输出

# 2022061804540300, Setting Wafer Attribute for 9791-10:WH77B189ESA5:GOOD DIE COUNT to 329
# 2022061804540300, Setting Wafer Attribute for 9791-12:WH77B195ESC7:GOOD DIE COUNT to 322
# 2022061804540300, Setting Lot Attribute GOOD DIE COUNT to 3585
# 2022061804540300, Setting Lot Attribute CURRENT QTY to 3585
# 2022061804540300, Setting Lot Attribute in Subcon section CURRENT QTY to 3585
# 2022061804540300, Updating DRF: /home/prbdata/tracking/prod/stage/dispo_results/2022061804535700_1864881_001_SLC_dispoinfo.xml
# 2022061804540300, No StartLot action for OFFLOAD - will not initiate dfs data transfer
# 2022061804540300, TRANSPORT_FILE = /home/prbdata/tracking/prod/transaction/outgoing/202206180400/2022061804535700_1864881_001_SLC_dispoinfo.xml
# 2022061804540300, cp: cannot create regular file ΓÇÿ/mads/tpp/spool/EngineeringToolbox/dispoResults/2022061804535700_1864881_001_SLC_dispoinfo.xmlΓÇÖ: Permission denied
# 2022061804540300, [EVENT::FATAL] 'PRBDATA_DFS_FAIL_COPY':'/home/prbdata/tracking/prod/transaction/outgoing/202206180400/2022061804535700_1864881_001_SLC_dispoinfo.xml':'/mads/tpp/spool/EngineeringToolbox/dispoResults'

这是我代码:

use Micron::Mail;
use Micron::Page;
use DBI;
use DBD::mysql;
use Time::HiRes qw( time );
use DateTime;

my $file =  "C:/Users/pphyuphway/Downloads/test123.txt";
my $file1 =  "C:/Users/pphyuphway/Downloads/test321.txt";

open( my $fh4, "<", $file) or die "Could not open file '$file' $!";
open( my $fh5, ">", $file1) or die "Could not open file '$file1' $!";
my $found = 0;
my @buf;
foreach my $lot (<$fh4>) {
   if ($lot =~/EVENT::FATAL/) {
     print("$lot\n");
       $found = 1;
       last;
   }

   push(@buf, $lot);
   shift(@buf) if @buf >= 10;
}
print(@buf) if $found;
#print {$fh5} "@buf\n" if $found;
csga3l58

csga3l581#

下面是代码的重要部分:

foreach my $lot (<$fh4>) {
   if ($lot =~/EVENT::FATAL/) {
     print("$lot\n");
       $found = 1;
       last;
   }

   push(@buf, $lot);
   shift(@buf) if @buf >= 10;
}

如果你的行包含“EVENT::FATAL”,你立即退出循环(使用last),这意味着循环体的最后两行不会被执行。这意味着包含“EVENT::FATAL”的行永远不会被添加到@buf中,因此不会被打印出来。
我建议您先执行@buf内务处理,然后再查看是否存在匹配项,并可能退出循环。

foreach my $lot (<$fh4>) {
   push(@buf, $lot);
   shift(@buf) if @buf >= 10;

   if ($lot =~/EVENT::FATAL/) {
     print("$lot\n");
       $found = 1;
       last;
   }
}
rn0zuynd

rn0zuynd2#

在命令行中,使用grep

grep -B10 'EVENT:FATAL' in_file

相关问题