RMGir has asked for the wisdom of the Perl Monks concerning the following question:
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):
(Note that I'm not using strict, not using -w. These are meant to be one-liners)perl -ne'print unless $seen{$_}++' filesToUniq
Print out all the seen values for a field (in this example, all the HTTP protocol versions used to access my web server)
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 unless $seen{$1}++' access_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...perl -ne'm[HTTP/([^"]+)] or next; print "$1\n" unless $seen{$1}++' acc +ess_log
Hmmm, maybe I should drop the 'een' and improve my golf score. Oops, wrong thread! :)
--
Mike
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Better "uniq" idiom?
by Juerd (Abbot) on Mar 20, 2002 at 17:16 UTC | |
by Sidhekin (Priest) on Mar 21, 2002 at 15:14 UTC | |
by Juerd (Abbot) on Mar 21, 2002 at 22:09 UTC | |
by RMGir (Prior) on Mar 20, 2002 at 17:21 UTC | |
|
Re: Better "uniq" idiom?
by perrin (Chancellor) on Mar 20, 2002 at 16:59 UTC | |
by RMGir (Prior) on Mar 20, 2002 at 17:04 UTC |