我正在尝试自动化一项任务,因为我正在阅读一个 txt 文件并搜索几个关键字。一旦我得到那个关键字,我就会从我得到那个关键字的行中打印几个指定的行(下面)。
我已经编写了代码,但是我能够打印单行而不是一堆行。
#!/usr/bin/perl
use strict;
use warnings;
open(my $fh,"<","dmesg_TZ.txt") or die"can not open the file";
open(my $fh_01,">","dmesg.txt") or die"can not open the file";
my @lines=<$fh>;
close($fh);
my @Kenel_panic_lines;
for(my $i=0;$i<=$#lines;$i++){
if($lines[$i] =~ m/Kernel panic/){
my $count=5;
do{
push(@Kenel_panic_lines,$lines[$i]);
$count--;
}until($count==0);
}
if($lines[$i] =~ m/Exception stack/){
my $count=10;
do{
push(@Kenel_panic_lines,$lines[$i]);
$count--;
}until($count==0);
print $fh_01 "@Kenel_panic_lines";
}
}
文件内容:
<3>[35591.866161] init: untracked pid 20769 exited
<3>[35591.890789] init: untracked pid 20766 exited
<3>[35591.894074] init: untracked pid 20768 exited
<3>[35591.917100] init: untracked pid 20770 exited
<3>[35591.925421] init: untracked pid 20771 exited
<3>[35591.928705] init: untracked pid 20772 exited
<3>[35591.938979] init: untracked pid 20773 exited
<3>[35591.942266] init: untracked pid 20774 exited
<3>[35591.946518] init: untracked pid 20775 exited
<3>[35591.958501] init: untracked pid 20776 exited
<3>[35591.962059] init: untracked pid 20777 exited
<3>[35591.966335] init: untracked pid 20778 exited
<3>[35591.975695] init: untracked pid 20779 exited
<3>[35606.088879] qpnp_adc_tm_channel_measure: No notification for high/low temp??
<3>[35607.088974] qpnp_iadc_calibrate_for_trim: qpnp adc result read failed with -22
<3>[35627.048894] qpnp_adc_tm_channel_measure: No notification for high/low temp??
<3>[35628.048945] qpnp_iadc_calibrate_for_trim: qpnp adc result read failed with -22
<3>[35648.048895] qpnp_adc_tm_channel_measure: No notification for high/low temp??
<3>[35649.048950] qpnp_iadc_calibrate_for_trim: qpnp adc result read failed with -22
<3>[35669.048901] qpnp_adc_tm_channel_measure: No notification for high/low temp??
<3>[35669.230782] init: untracked pid 21275 exited
<3>[35669.237371] init: untracked pid 21272 exited
<3>[35669.244689] init: untracked pid 21273 exited
<3>[35669.252667] init: untracked pid 21274 exited
<3>[35670.048942] qpnp_iadc_calibrate_for_trim: qpnp adc result read failed with -22
<3>[35690.048925] qpnp_adc_tm_channel_measure: No notification for high/low temp??
<3>[35691.048995] qpnp_iadc_calibrate_for_trim: qpnp adc result read failed with -22
<3>[35712.088913] qpnp_adc_tm_channel_measure: No notification for high/low temp??
<3>[35713.089001] qpnp_iadc_calibrate_for_trim: qpnp adc result read failed with -22
<3>[35732.201384] init: untracked pid 21306 exited
<3>[35732.204671] init: untracked pid 21307 exited
<3>[35732.244679] init: untracked pid 21308 exited
<3>[35732.247964] init: untracked pid 21309 exited
<3>[35732.269020] init: untracked pid 21310 exited
<3>[35732.272305] init: untracked pid 21311 exited
<3>[35732.289071] init: untracked pid 21305 exited
<3>[35732.299140] init: untracked pid 21312 exited
<3>[35732.302426] init: untracked pid 21313 exited
<3>[35732.312214] init: untracked pid 21314 exited
<3>[35732.319164] init: untracked pid 21315 exited
<3>[35732.322447] init: untracked pid 21316 exited
<3>[35732.337583] init: untracked pid 21317 exited
<3>[35732.343204] init: untracked pid 21318 exited
<3>[35734.088887] qpnp_adc_tm_channel_measure: No notification for high/low temp??
<3>[35735.088984] qpnp_iadc_calibrate_for_trim: qpnp adc result read failed with -22
<4>[35737.481469] QSEECOM: qseecom_release: data->released == false
<1>[35742.353244] L2 Error detected!
<1>[35742.355266] L2ESR = 0x00010001
<1>[35742.358650] L2ESYNR0 = 0x37000142
<1>[35742.362036] L2ESYNR1 = 0x08b02602
<1>[35742.365419] L2EAR0 = 0x48010034
<1>[35742.368806] L2EAR1 = 0x00000003
<1>[35742.372190] CPU bitmap = 0x1
<1>[35742.375141] L2 master port decode error
<6>[35742.404857] coresight-tmc fc307000.tmc: TMC aborted
<0>[35742.408698] Kernel panic - not syncing: L2 master port error detected
<4>[35742.415136] [<c0013e44>] (unwind_backtrace+0x0/0x11c) from [<c0793994>] (panic+0x88/0x1ec)
<4>[35742.423372] [<c0793994>] (panic+0x88/0x1ec) from [<c008fb1c>] (msm_l2_erp_irq+0x1f4/0x260)
<4>[35742.431619] [<c008fb1c>] (msm_l2_erp_irq+0x1f4/0x260) from [<c00e7b7c>] (handle_irq_event_percpu+0xb0/0x290)
<4>[35742.441427] [<c00e7b7c>] (handle_irq_event_percpu+0xb0/0x290) from [<c00e7d98>] (handle_irq_event+0x3c/0x5c)
<4>[35742.451237] [<c00e7d98>] (handle_irq_event+0x3c/0x5c) from [<c00ea860>] (handle_fasteoi_irq+0xdc/0x148)
<4>[35742.460611] [<c00ea860>] (handle_fasteoi_irq+0xdc/0x148) from [<c00e753c>] (generic_handle_irq+0x20/0x30)
<4>[35742.470160] [<c00e753c>] (generic_handle_irq+0x20/0x30) from [<c000e840>] (handle_IRQ+0x7c/0xc0)
<4>[35742.478926] [<c000e840>] (handle_IRQ+0x7c/0xc0) from [<c00086c8>] (gic_handle_irq+0x94/0x110)
<4>[35742.487435] [<c00086c8>] (gic_handle_irq+0x94/0x110) from [<c07a4740>] (__irq_svc+0x40/0x70)
<4>[35742.495849] Exception stack(0xdae0fcc0 to 0xdae0fd08)
<4>[35742.500886] fcc0: 00000001 ffffffaa 0000008a f06c91c0 f08aa208 00000000 00000000 00000200
<4>[35742.509044] fce0: dae0fd98 edf5bcf0 00000254 ed18ca90 00000000 dae0fd08 c008f108 c047334c
<4>[35742.517202] fd00: 20000013 ffffffff
<4>[35742.520682] [<c07a4740>] (__irq_svc+0x40/0x70) from [<c047334c>] (msm_camera_io_w+0x30/0x3c)
<4>[35742.529106] [<c047334c>] (msm_camera_io_w+0x30/0x3c) from [<c0487288>] (msm_ispif_reset+0x4c/0x1a0)
<4>[35742.538128] [<c0487288>] (msm_ispif_reset+0x4c/0x1a0) from [<c0487414>] (msm_ispif_release+0x38/0xb8)
<4>[35742.547329] [<c0487414>] (msm_ispif_release+0x38/0xb8) from [<c0488464>] (msm_ispif_subdev_ioctl+0xa44/0xb10)
<4>[35742.557223] [<c0488464>] (msm_ispif_subdev_ioctl+0xa44/0xb10) from [<c046fb30>] (msm_close+0x94/0x190)
<4>[35742.566514] [<c046fb30>] (msm_close+0x94/0x190) from [<c049fc54>] (v4l2_release+0x44/0x64)
<4>[35742.574762] [<c049fc54>] (v4l2_release+0x44/0x64) from [<c01500d4>] (fput+0x11c/0x228)
<4>[35742.582659] [<c01500d4>] (fput+0x11c/0x228) from [<c014cc64>] (filp_close+0x70/0x7c)
<4>[35742.590385] [<c014cc64>] (filp_close+0x70/0x7c) from [<c0096fb0>] (put_files_struct+0x90/0x100)
<4>[35742.599063] [<c0096fb0>] (put_files_struct+0x90/0x100) from [<c0097344>] (do_exit+0x2a8/0x828)
<4>[35742.607658] [<c0097344>] (do_exit+0x2a8/0x828) from [<c0097b64>] (do_group_exit+0x88/0xb4)
<4>[35742.615904] [<c0097b64>] (do_group_exit+0x88/0xb4) from [<c00a4af8>] (get_signal_to_deliver+0x510/0x568)
<4>[35742.625368] [<c00a4af8>] (get_signal_to_deliver+0x510/0x568) from [<c0010a14>] (do_signal+0xc4/0x4d8)
<4>[35742.634569] [<c0010a14>] (do_signal+0xc4/0x4d8) from [<c0011294>] (do_notify_resume+0x18/0x50)
<4>[35742.643160] [<c0011294>] (do_notify_resume+0x18/0x50) from [<c000dfd8>] (work_pending+0x24/0x28)
<3>[35744.416807] wcnss crash shutdown 0
<0>[35744.419180] Rebooting in 5 seconds..
<5>[35749.424117] Going down for restart now
sion: 0x312e3176