title: Succinct way to print all lines up until the last line that matches a given pattern
body: <p>I'm trying to find a succinct shell one-liner that'll give me all the
  lines in a file <em>up until</em> some pattern.</p>
  
  <p>The use case is dumping all the lines in a log file until I spot some
  marker indicating that the server has been restarted.</p>
  
  <p>Here's a stupid shell-only way that:</p>
  
  <pre><code>tail_file_to_pattern() {
      pattern=$1
      file=$2
  
      tail -n$((1 + $(wc -l $file | cut -d' ' -f1) - $(grep -E -n "$pattern" $file | tail -n 1 | cut -d ':' -f1))) $file
  }
  </code></pre>
  
  <p>A slightly more reliable Perl way that takes the file on stdin:</p>
  
  <pre><code>perl -we '
      push @lines =&gt; $_ while &lt;STDIN&gt;;
      my $pattern = $ARGV[0];
      END {
          my $last_match = 0;
          for (my $i = @lines; $i--;) {
              $last_match = $i and last if $lines[$i] =~ /$pattern/;
          }
          print @lines[$last_match..$#lines];
      }
  '
  </code></pre>
  
  <p>And of course you could do that more efficiently be opening the file,
  seeking to the end and seeking back until you found a matching line.</p>
  
  <p>It's easy to print everything as of the <em>first</em> occurrence, e.g.:</p>
  
  <pre><code>sed -n '/PATTERN/,$p'
  </code></pre>
  
  <p>But I haven't come up with a way to print everything as of the <em>last</em>
  occurance.</p>