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

发布于 2025-02-09 02:49:11 字数 4470 浏览 1 评论 0原文

我想打印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,

因此,如果在文件中发现“事件:致命”字符串,它将在上方打印10行,包括“事件:致命”行。

我的输出文件:

# 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;

i would like to print 10 lines including specific string line. my code only print 10 line above , it cannot print line which include specific string. kindly help to suggest below is my txt file data.

# 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,

so if "EVENT:FATAL" string found in file, it will print 10 lines above including "EVENT:FATAL" line.

my output file :

# 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

expected file output

# 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'

this is my code:

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶底世界 2025-02-16 02:49:12

这是您代码的重要部分:

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

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

如果您的行包含“事件::致命”,则立即退出循环(使用last),这意味着未执行循环的最后两行。这意味着包含“事件::致命”的行永远不会添加到@buf,因此不会被打印出来。

我建议您在查看是否有匹配并可能退出循环之前,进行@buf家政服务。

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

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

Here's the important part of your code:

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

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

If your line contains "EVENT::FATAL", you exit the loop immediately (using last) which means that the final two lines of the loop body aren't executed. This means that the line containing "EVENT::FATAL" is never added to @buf and, therefore doesn't get printed out.

I suggest you do the @buf housekeeping before seeing if you have a match and potentially exiting the loop.

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

   if ($lot =~/EVENT::FATAL/) {
     print("$lot\n");
       $found = 1;
       last;
   }
}
小忆控 2025-02-16 02:49:12

在命令行上,使用

grep -B10 'EVENT:FATAL' in_file

On the command line, use grep:

grep -B10 'EVENT:FATAL' in_file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文