I put this on my scratchpad about a month back:

Cool vi macro: @p runs Perl code between #! and __END__ lines on any text after the __END__, preserving the perl code. The "let" command to set @p only works in vim, but the macro (drop the quotes and replace \r with ^V^M -- probably by typing ^V^V^V^M) works in any reasonable vi-alike.

:let @p="L?^#!\r/^__END__\ry''''p!Gperl\rG"

L goes to the bottom of the current screen so that the next bit is unlikely to fail (which would halt the macro); ?^#! searches for the first line of the 'script'; /^__END__ searches for the end of what needs to be pasted because it won't be output when perl is run; y'' yanks from the __END__ through the #! line; '' jumps back to the __END__; p pastes the perl code (so the original copy above can remain in the editor buffer); !Gperl runs the bottom of the file (the freshly pasted Perl code and the text to be filtered). G goes to the bottom of the results after filtering.

For example, when processing error logs (which are usually too verbose for any one task), I'll trim the log repeatedly, often reusing parts of saved trimming code (in part because I prefer Perl regexes). Part of the value of this method is if I get such code wrong, I can just "u" (undo), adjust the code, and try again. (But I also often have filters that makes sense to apply more than once.)

#!/usr/bin/perl -n next if /.../; s/.../.../; s/.../.../; if( /.../ ) { my( $date, $time )= /(\d([-/\d]+\d) (\d([\d:.]+\d)/; my( $hr, $min, $sec, $ms )= split /[:.]/, $time; my $now= $ms/1000 + $sec + 60*( $min + 60*$hr ); if( $Then ) { $sec= $now - $Then; $min= int( $sec / 60 ); $sec -= 60*$min; $_= sprintf "+%d:%06.3f %s", $min, $sec, $_; } $Then= $now; } print; __END__ ...

- tye        


In reply to Re: Contribute a hack to the new "Perl Hacks" book (filter) by tye
in thread Contribute a hack to the new "Perl Hacks" book by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.