SPUG:Best One-Liners and Scripts for UNIX
Tim Maher
tim at consultix-inc.com
Fri Apr 18 00:42:40 CDT 2003
On Thu, Apr 17, 2003 at 07:07:16AM -0700, Brian Hatch wrote:
>
>
> function sp {
> fields=$1
> shift;
> $PERL5 -ape 'chomp @F; $_=join(" ",grep /./, @F['$fields'])."\n"' $@
> }
> Brian Hatch "Beep! Beep! Nipple bus!
Ouch! That's too complicated for my "Minimal Perl" tastes.
How about the following as a one-liner to type directly to the shell,
perl -wlna -e 'print "@F[3,1,2]"' file
or the following as a more civilized scriptified version:
#! /usr/bin/perl -wlna
BEGIN {
# first argument contains comma-separated field numbers
# order of numbers determines order of printing fields
$fields = shift;
@fields = $fields =~ /\d+/g or
die "Usage: $0 '2,1,3' [ file ... ]";
# The more obvious parsing solution passes non-digits - BAD!
# @fields=split /,/ , $fields or
}
@F or next; # skip blank lines
# User should specify from #1
unshift @F, "(No zeroth field! first is #1)";
print "@F[ @fields ]";
-Tim
*------------------------------------------------------------*
| Tim Maher (206) 781-UNIX (866) DOC-PERL (866) DOC-UNIX |
| CEO, JAWCAR ("Just Another White Camel Award Recipient") |
| tim at Consultix-Inc.Com TeachMeUnix.Com TeachMePerl.Com |
*+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
| Watch for my Book: "Minimal Perl for Shell Programmers" |
*------------------------------------------------------------*
More information about the spug-list
mailing list