如果在文件中找到特定的字符串,如何在上方打印10行?
我想打印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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您代码的重要部分:
如果您的行包含“事件::致命”,则立即退出循环(使用
last
),这意味着未执行循环的最后两行。这意味着包含“事件::致命”的行永远不会添加到@buf
,因此不会被打印出来。我建议您在查看是否有匹配并可能退出循环之前,进行
@buf
家政服务。Here's the important part of your code:
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.在命令行上,使用 :
On the command line, use
grep
: