Home > Software design >  How to print 10 lines above if specific string found in a file?
How to print 10 lines above if specific string found in a file?

Time:06-21

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;

CodePudding user response:

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;
   }
}
  •  Tags:  
  • perl
  • Related