简体   繁体   English

在Perl中,如何输出(打印)字符串之后的所有行?

[英]In Perl, how do I output (print) all lines that follow a string?

I need to process a log file. 我需要处理一个日志文件。 I send it to Perl via <STDIN> . 我通过<STDIN>将其发送到Perl。 I need to output only the summary, which follows a line that contains several equal signs. 我只需要输出摘要,该摘要紧随包含多个等号的一行。

The summary is the last bit in the daily log. 摘要是每日日志中的最后一位。 So, I need everything starting after the line with the = s through EOF. 因此,我需要从EOF到= s行之后的所有内容。

I tried using a while loop with a " next unless " but I could not get that to work. 我尝试将while循环与“ next unless ”一起使用,但无法正常工作。

You need a flag or two loops. 您需要一个标志或两个循环。


Includes the trigger line in the output: 在输出中包括触发线:

perl -e'
   while (<>) {
      last if /===/;
   }

   if (defined($_)) {
      print;
      while (<>) {
         print;
      }
   }
' log

perl -ne'print if $print ||= /===/' log

Doesn't include the trigger line in the output: 在输出中不包含触发线:

perl -e'
   while (<>) {
      last if /===/;
   }

   if (defined($_)) {
      while (<>) {
         print;
      }
   }
' log

perl -ne'last if /===/; END { print while <> }' log

perl -ne'print if $print; $print ||= /===/' log

perl -ne'print unless 1 .. /===/' log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM