Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Succint Perl version of Awk

by Galdor (Sexton)
on May 30, 2023 at 05:46 UTC ( [id://11152493]=perlquestion: print w/replies, xml ) Need Help??

Galdor has asked for the wisdom of the Perl Monks concerning the following question:

Is there an elegant and just as succinct way of calling this in Perl (in a MinPerl fashion - to be used in a pipe)?
awk '{ $1=$2=$3=""; print $0 }'
ie loose the first three columns and print the rest? (note I use this to wrangle my history file - HISTTIMEFORMAT="%F %T "). (Found "better" Perl one-liners for all awk commans so far but not this one). PS - the awk solution has an annoying first three spaces on each line - bonus would be to get rid of that too.

Replies are listed 'Best First'.
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

      yeah - I guess it was that 2d variant I was looking for...More perlish...Thanks.
Re: Succint Perl version of Awk
by choroba (Cardinal) on May 30, 2023 at 08:03 UTC
    TIMTOWTDI:
    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]

      Taking advantage that the warnings are off:

      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]"'
      LOL - thaksf or your intransigence! it has been noted and appended to my Perl scrapbook... :)
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
      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..
Re: Succint Perl version of Awk
by kcott (Archbishop) on May 30, 2023 at 14:12 UTC

    G'day Galdor,

    TMTOWTDI:

    $ 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.

    — Ken

Re: Succint Perl version of Awk
by Utilitarian (Vicar) on May 30, 2023 at 11:27 UTC
    Just print the bits you want:
        perl -F -e 'print join(" ",@F[3..$#F])' sample.csv

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11152493]
Approved by GrandFather
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-28 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found