Re: more on 1 liner perl/awk
Colin, you are the king.
On Mon, Mar 10, 2003 at 09:18:05PM +0000, Colin Watson wrote:
> On Mon, Mar 10, 2003 at 12:26:47PM -0800, Osamu Aoki wrote:
> > Another guy replied with
> (All of this is untested.)
Here is my test results.
> Well, you can clearly save some keystrokes by using a postfix statement
> modifier rather than a block:
>
> perl -ne 'print "$f[2]\n" if (@f = split)[1] eq "1957"' < mat.txt
Good.
> You can simplify the print using the -l switch:
>
> perl -lne 'print $f[2] if (@f = split)[1] eq "1957"' < mat.txt
Good.
> Then you could use -a to do the split for you (use -F'\t' as well if the
> default behaviour of splitting on whitespace is too loose for you):
>
> perl -lane 'print $F[2] if $F[1] eq "1957"' < mat.txt
Good.
> Then there are the usual tricks of eliminating whitespace and so on. In
> fact, all the whitespace in the arguments to perl in the line above can
> be removed if you feel so inclined:
>
> perl -lane'print$F[2]if$F[1]eq"1957"' < mat.txt
Darn. This is good too.
> Shorter solutions may be possible.
>
> Google for "Perl Golf" if you're interested in this kind of thing: the
> art of solving problems in the fewest (key)strokes.
http://perlgolf.sourceforge.net/
Amazing :-)
--
~\^o^/~~~ ~\^.^/~~~ ~\^*^/~~~ ~\^_^/~~~ ~\^+^/~~~ ~\^:^/~~~ ~\^v^/~~~ +++++
Osamu Aoki <osamu@debian.org> Cupertino CA USA, GPG-key: A8061F32
.''`. Debian Reference: post-installation user's guide for non-developers
: :' : http://qref.sf.net and http://people.debian.org/~osamu
`. `' "Our Priorities are Our Users and Free Software" --- Social Contract
Reply to: