Re: more on 1 liner perl/awk
Hi, (back to list)
On Mon, Mar 10, 2003 at 12:25:18AM -0800, Alvin Oga wrote:
> On Mon, 10 Mar 2003, Osamu Aoki wrote:
> > On Sun, Mar 09, 2003 at 11:37:00PM -0800, Alvin Oga wrote:
> > > i say its ( a wild guess )
> > >
> > > perl -ne 'print (split('\t'))[2] if (split('\t'))[1]==1957' ;
> >
> > That was my wild guess too and booo.... No good.
>
> hummm...
>
> we can cheat ???
>
> perl -ne ' @f=split(\t) ; print $f[2] if ( $f[1]==1957) ' ;
>
> with the proper end/start and escape chars
> ( i've never combined 2 perl instruc in the same command line )
Another guy replied with
perl -ne 'if ((my @fields = split)[1] eq "1957") { print "$fields[2]\n" }'
I tested all:
osamu@goofy:osamu$ perl -ne 'if ((my @f = split)[1] eq "1957") { print "$f[2]\n" }' < mat.txt
111
osamu@goofy:osamu$ perl -ne '@f = split; if ($f[1] eq "1957") { print "$f[2]\n" }' < mat.txt
111
osamu@goofy:osamu$ perl -ne 'if ((@f = split)[1] eq "1957") { print "$f[2]\n" }' < mat.txt
111
For 1 liner, I guess local variable does not make much difference. So
the last one is shortest.
Does anyone else has shorter trick?
I like Alvin's since easier to read by having one more character at this
moment.
--
~\^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: