Re: awk...ward
by jdporter (Paladin) on Feb 24, 2005 at 19:38 UTC
|
perl -lane "print $F[1]"
See perldoc perlrun.
Update: On Windows, you have to use double-quotes to enclose a command-line script.
On Unix, there'll be a problem with the double-quotes and the dollar sign. Therefore, on Unix,
I'd recommend using single-qoutes if possible; or otherwise, escape the dollar sign.
| [reply] [d/l] |
|
|
perl -lane "print $F[1]"
or if we're golfing here,
perl -lape'$_=$F[1]'
Dave. | [reply] [d/l] [select] |
|
|
| [reply] [d/l] |
|
|
D'oh. Well, I'm not ashamed to admit my awk skills are a little rusty.
Perl has been my text processing tool of first resort for the last... oh, 11 years. :-)
| [reply] |
Re: awk...ward
by Roy Johnson (Monsignor) on Feb 24, 2005 at 20:28 UTC
|
The a2p program that is included with perl will translate awk scripts to perl. It can be instructive to look at the translations, if your original script wasn't too complex. (For complex awk scripts, the translation soon gets too cumbersome to plow through.)
% cat try.awk
{print $1}
% a2p try.awk > try.perl
% cat try.perl
#!/usr/shell/bin/perl
eval 'exec perl -S $0 "$@"'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)
eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches
$, = ' '; # set output field separator
$\ = "\n"; # set output record separator
while (<>) {
($Fld1) = split(' ', $_, 9999);
print $Fld1;
}
Caution: Contents may have been coded under pressure.
| [reply] [d/l] |
|
|
Except in this case. That's some pretty nasty perl for what it's trying to do. I've previously contemplated writing a module to somehow allow awk column sementics in perl... never quite got a round tuit though.
| [reply] |
|
|
Inline::Awk gives you all the power (?!) of awk from within a Perl program:
perl -le 'use Inline Awk=>q({print $1}); awk()' file
I agree with you that the output of a2p isn't pretty and it is probably not the best way for an awk programmer to learn Perl.
--
John.
| [reply] [d/l] |
|
|
Re: awk...ward
by friedo (Prior) on Feb 24, 2005 at 19:34 UTC
|
To split on whitespace you can use (*grin*) split.
perl -le 'print((split " ", "best ever")[0])'
Or, you could grab it with a regex.
perl -le '"best ever" =~ /(\w+)/; print $1'
| [reply] [d/l] [select] |
Re: awk...ward
by eyepopslikeamosquito (Archbishop) on Feb 24, 2005 at 22:58 UTC
|
Though I generally much prefer Perl to awk, in this case
I prefer your awk one-liner above to the equivalent Perl
one-liner -- if calling from a /bin/sh script.
My main suggestion is to write the whole script, not in /bin/sh, but Perl; then you don't need
all these little one-liners. The only time I
write /bin/sh over Perl is for truly tiny scripts or when
I need the script to run on a
machine that does not have Perl installed.
| [reply] |
Re: awk...ward
by RazorbladeBidet (Friar) on Feb 24, 2005 at 19:35 UTC
|
| [reply] [d/l] [select] |
Re: awk...ward
by ambrus (Abbot) on Dec 26, 2007 at 11:04 UTC
|
| [reply] |