Roger Creasy
Roger Creasy

Reputation: 1529

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>. 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.

I tried using a while loop with a "next unless" but I could not get that to work.

Upvotes: 0

Views: 118

Answers (1)

ikegami
ikegami

Reputation: 385655

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

Upvotes: 4

Related Questions