I have an idiom I use a lot to do quick searches through log files or data streams, or to do a "uniq" on a file without having to sort it.

I'm wondering if someone can suggest a better way to do it, out of curiosity. If no one has a better way, then here's my way, you're welcome to use it :)

Simple "uniq" (print unique lines):

perl -ne'print unless $seen{$_}++' filesToUniq
(Note that I'm not using strict, not using -w. These are meant to be one-liners)

Print out all the seen values for a field (in this example, all the HTTP protocol versions used to access my web server)

perl -ne'm[HTTP/([^"]+)] or next; print unless $seen{$1}++' access_log
That version prints out the first line on which a given protocol is encountered. If you prefer just enumerating the protocols, use:
perl -ne'm[HTTP/([^"]+)] or next; print "$1\n" unless $seen{$1}++' acc +ess_log
This is hardly "perl rocket science", obviously. But I figured it might be useful, since I wind up typing "$seen{$_}++" a lot every day...

Hmmm, maybe I should drop the 'een' and improve my golf score. Oops, wrong thread! :)
--
Mike


In reply to Better "uniq" idiom? by RMGir

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.