Re: Succint Perl version of Awk
by eyepopslikeamosquito (Archbishop) on May 30, 2023 at 06:32 UTC
|
$ cat jwtest.txt
one two three four five six seven eight nine
A B C D E F G H
A B C D E F G H I J
$ perl -ale'$F[0]=$F[1]=$F[2]=""; print "@F"' <jwtest.txt
four five six seven eight nine
D E F G H
D E F G H I J
$ perl -alpe '$_="@F[3..$#F]"' <jwtest.txt
four five six seven eight nine
D E F G H
D E F G H I J
| [reply] [Watch: Dir/Any] [d/l] |
|
yeah - I guess it was that 2d variant I was looking for...More perlish...Thanks.
| [reply] [Watch: Dir/Any] |
Re: Succint Perl version of Awk
by choroba (Cardinal) on May 30, 2023 at 08:03 UTC
|
perl -lane '@F[0,1,2] = ("") x 3; print "@F"'
perl -lane 'splice @F, 0, 3, ("") x 3; print "@F"'
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
perl -lane'@F[0..2] = (); print "@F"'
Golfed:
perl -pale'$_=" @F[3..$#F]"'
Note that these solutions, like those in the parent, don't "lose the first three columns". They just replace their values with blanks. Just like the awk program does. If you truly want to remove the leading columns (i.e. no leading spaces in the output), then you can use
perl -pale'$_="@F[3..$#F]"'
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
LOL - thaksf or your intransigence!
it has been noted and appended to my Perl scrapbook... :)
| [reply] [Watch: Dir/Any] |
Re: Succint Perl version of Awk
by jwkrahn (Abbot) on May 30, 2023 at 06:13 UTC
|
$ echo "one two three four five six seven eight nine
A B C D E F G H
A B C D E F G H I J" | awk '{ $1=$2=$3=""; print $0 }'
four five six seven eight nine
D E F G H
D E F G H I J
$ echo "one two three four five six seven eight nine
A B C D E F G H
A B C D E F G H I J" | perl -ale'$F[0]=$F[1]=$F[2]=""; prin
+t "@F"'
four five six seven eight nine
D E F G H
D E F G H I J
Naked blocks are fun!
-- Randal L. Schwartz, Perl hacker
| [reply] [Watch: Dir/Any] [d/l] |
|
thanks for that...I had more or less come up with similar - but kept reverting to awk simply because it was shorter and easier to type (especially in cli pipes!) - the awk solution is very elegant compared to the Perl in this example - and Perl here, as ever, is capable...but in this case also cumbersome (atypical). Thanks for sample code..
| [reply] [Watch: Dir/Any] |
Re: Succint Perl version of Awk
by kcott (Archbishop) on May 30, 2023 at 14:12 UTC
|
$ echo "one two three four five six seven eight nine
A B C D E F G H
A B C D E F G H I J" | perl -pe 's/^(?:\s*\S+\s+){3}//'
four five six seven eight nine
D E F G H
D E F G H I J
Update: Added a '^' anchor to the regex.
Was: s/(?:\s*\S+\s+){3}//
Now: s/^(?:\s*\S+\s+){3}//
It doesn't change the result; but looks more correct.
With a huge history file, it might be a little quicker.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Succint Perl version of Awk
by Utilitarian (Vicar) on May 30, 2023 at 11:27 UTC
|
| [reply] [Watch: Dir/Any] [d/l] [select] |