Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

One-liner's quoting and subroutine

by reisinge (Hermit)
on Sep 27, 2016 at 05:42 UTC ( #1172707=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks :-), I have two questions related to the following one-liner:

find /opt/splunk/syslog/ -iname "*log*" -type f -mtime +30 | perl -wne + 'BEGIN { $sum = 0 }; chomp; $sum += (stat)[7]; END { print "$sum\n" +}'

1) How should I quote it (escape the single quotes) when I want to execute it on a remote machine via SSH? Ex. ssh root@HOST 'ONE-LINER'

2) Is there is a (smart) way to incorporate the following subroutine into the one-liner?

sub scaleIt { my $size_in_bytes = shift; return unless defined $size_in_bytes; my ( $size, $n ) = ( $size_in_bytes, 0 ); ++$n and $size /= 1024 until $size < 1024; return sprintf "%.0f%s", $size, (qw[ B KB MB GB TB ])[$n]; }

You can talk and talk and have great ideas, but if you don't have a way of sharing those ideas then you are just saying it in you own bubble. -- Tom Limoncelli

Replies are listed 'Best First'.
Re: One-liner's quoting and subroutine
by Discipulus (Abbot) on Sep 27, 2016 at 08:17 UTC
    Probably is wiser to turn the whole thing into a script, (update: and run it remotely) i think ssh is able to do this ;=) you also eliminate the quoting problem.

    I like oneliners but, please take a read of the recent Meditation covering pro and cons of oneliners.

    For your curiosity you can cut a lot your Perl oneliner: the whole BEGIN { $sum = 0 }; is unneeded: without using strict you can freely use a variable even if not initialized so when you do $first_time_seen += 1; Perl knows that you want a number, if it is not initialized Perl choose 0 as wise precognition, then add 1 and all is good. Also ; is unneeded after a block declaration.

    The chomp instruction too is unnecessary: you never print the name of the file, but anyway if in oneliner you are dealing with newlines -l switch is your friend! see perlrun.

    You can also cheat to not type the END block at all using the eskimo greeting operator. If so the original oneliner becomes a lot shorter: something like:

    perl -wne '$sum += (stat)[7]}{print "$sum\n"'

    If you want to put the "human readable sizes" code the oneliner become suddenly fat and unfriendly: the best I arrived is, in the END created by the eskimo greeting:

    @suf=qw(B KB MB GB TB);while($sum>=1024){$sum/=1024;shift @suf}prin +t $sum," ",shift @suf

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      I like how you shortened the original one-liner, thanks :-). However, I had to modify it so it works on my system:

      perl -lne '$sum += (stat)[7]}{print $sum'
Re: One-liner's quoting and subroutine
by Happy-the-monk (Canon) on Sep 27, 2016 at 07:59 UTC

    1) How should I quote it (escape the single quotes) when I want to execute it on a remote machine via SSH? Ex. ssh root@HOST 'ONE-LINER'

    Qou may use quoting operataors where my $example = 'first'; becomes my $example = q(first);

    2) Is there is a (smart) way to incorporate the following subroutine into the one-liner?

    You could sqeeze it on one line, or you can put it in a file and make it a module, than upload it to the host, later use that module in the one-liner?

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

Re: One-liner's quoting and subroutine
by Anonymous Monk on Sep 27, 2016 at 07:35 UTC
    turn the oneliner into a multiline program, use B::Deparse to help you

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-07-05 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?