Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Favourite One-liners?

by ghenry (Vicar)
on Jun 27, 2005 at 21:52 UTC ( #470397=perlmeditation: print w/ replies, xml ) Need Help??

Would you be so kind as to share your favourite one-liners?

Update:
I always use:

perl -e 'print <>' filename

Once we have plenty to chose from, I'll make up a one-liner cheat sheet.

Update2:
I've had a request to ask if you could all explain what the one-liners do as well.

Thanks.

Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!

Comment on Favourite One-liners?
Download Code
Re: Favourite One-liners?
by etcshadow (Priest) on Jun 27, 2005 at 22:11 UTC
    Awesome meditation. I have so many good ones that I don't even know where to start. Perhaps with the most pathological thing in my .bashrc?
    alias ww='watch '\''ptyexec w | perl -pe '\''\'\'''\''(print(" -1 "),n +ext) if $x++<2; ($i) = ($_ =~ /^.{42}\s+(\S+)/); $i=~/days/&&(print(" + 9999999999 "),next); $i=~/(?:(\d+):)?([\d.]+)([md]?)/; ($t=$1*60+$2) +; $3&&($t*=60); print sprintf(" %9d ",$t)'\''\'\'''\'' | sort -n | pe +rl -pe '\''\'\'''\''s/^\s+\S+\s+//'\''\'\'''\'''\'''
    where ptyexec is this. (You can just drop the ptyexec, though, if it gives trouble, it'll just truncate the output width to 80 chars, which is a little ugly).

    There's the ever-popular:

    perl -pi -e 's/\r\n?/\n/' *
    For fixing line-ends.

    And another simple one that I love because of how it demonstrates a neat concept is just:

    something | perl -lne 'END{print$x}$x+=$_'
    (better known as "sum").

    Update: added trailing slash to the line-end fixing regex. Thanks, tlm

    ------------ :Wq Not an editor command: Wq
Re: Favourite One-liners?
by chas (Priest) on Jun 27, 2005 at 22:28 UTC
    I think the first I learned was: perl -pe 0 "filename". Rather trivial, but it took a bit of thought to understand what the "0" was.
    For illustrating the power of Perl, I like perl-MLWP::Simple -e "print get(shift)" http://www.perlmonks.org

    chas
    (Those are for Win; Single quotes may be better on *nix.)
      If you're using ActiveState, just do
      GET http://www.perlmonks.org/
        Er, what does using activestate have to do with the ability to execute the GET program (which comes with LWP) ?
      FYI LWP::Simple can get and print with one command:
      perl -MLWP::Simple -e "getprint 'http://www.perlmonks.org'"
      Here's a webserver, from the IO::All docs:
      perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io(-x $1 +? "./$1 |" : $1) if /^GET \/(.*) / })'

      --
      perl -MO=Deparse -e"u j t S n a t o e h r , e p l r a h k c r e"

        One line web server? Way cool.

        A couple of years ago I went back to uni for a network communication class I failed once. After completing the lab assignment of writing a "time-of-day" server in C, I showed the lab instructor the equivalent perl code (one-liner). He thought it was pretty cool (I know, the lab goal is not the end result, but the methods you use to get there, but still..)

        Had I known about IO::All and one-line web servers at the time, I might have gotten extra points on the lab assignment :)

Re: Favourite One-liners?
by Joost (Canon) on Jun 27, 2005 at 22:34 UTC
Re: Favourite One-liners?
by jacques (Priest) on Jun 27, 2005 at 22:43 UTC
    perl -MSome::Module -e "END { foreach ( sort keys %INC ) { print \"$_\n\" } }";
      That's helpful for showing the dependencies (and structure) of a module; if you just want to know the file associated with a particular module, you can use perldoc -l Module::Name.

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Favourite One-liners?
by trammell (Priest) on Jun 27, 2005 at 23:31 UTC
    % perl -MCPAN -e shell

    Update: Thanks, BDF. Tidbits like this are why I hang around PM.

      Perl comes with a "cpan" script that does that for you so you don't have to do all that typing. You can also use it just to install modules quickly:

      cpan Foo::Bar Baz Quuz::Frob
      --
      brian d foy <brian@stonehenge.com>
Re: Favourite One-liners?
by diotalevi (Canon) on Jun 27, 2005 at 23:40 UTC

    It is impossible for me to have a favorite one-liner. Anything I have to type often enough to become a "favorite" would have already been comitted to a script so I wouldn't have to recreate it every time.

    That said, your code perl -e 'print <>' filename is a pathological form of cat filename, type filename, or perl -pe1 filename. You load the entire file into memory before printing it while the operation should normally only load a single line at a time (ignore the buffer, please). You're wasting tons of memory when you do that and for no good reason.

      The reason is just because I always remember it. No other reason than that.

      There are plenty of other versions available if I was really concerned about it though, but thanks anyway.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!
Re: Favourite One-liners?
by etcshadow (Priest) on Jun 27, 2005 at 23:58 UTC
    OH! And how can I forget this puppy. I know there probably aren't that many monks out there who fool around with X12 data files very often... but it is super sweet.

    I actually built a little shell-script around it as follows:

    #!/bin/sh # since "perl -p ... -n ..." will be treated by perl as just # "perl -p ... ..." (not allowing the -n to override the -p) # we have to scan through any switches on the command line, # so that commands like "x12cat -ne 'print if /^PLB/' ..." # will work as expected... but still allow an implicit "-p" # if no "-n" is specified. Granted this isn't perfect... # But this isn't so bad, because an explicit "-p" can override # this. looptype="" for i do case "$i" in -m*) ;; # -m and -M start command-line "use" direct +ives, -M*) ;; # "n"s in them should be ignored -*n*) looptype=n;; -h) echo 'x12cat: "magic" x12 spooler wrap +per around command-line perl. -splits files by segment terminator -auto sets $f and $c to (regex-escaped) field and component separato +rs -auto sets the autosplit to the field separator (for the -a autospli +t) -allows -pe or -ne operations on file (or none at all to just cat th +e file)'; exit;; -*) ;; *) break;; esac done if [ "x$looptype" = "x" ]; then looptype=p fi perl -l -$looptype -e 'BEGIN{$SIG{PIPE}="exit"; $|=1; $/=\108; ($f,$c, +$/)=(<> =~ /^ISA(.).{100}(.)(.?\r?\n?)/); ($f,$c)=("\Q$f","\Q$c"); se ek ARGV,0,0; $.=0}' -F'/$f/' "$@"
    Makes me so happy. And then (if it isn't clear), I compose one-liners off of that wrapper (called x12cat). Like:
    x12cat -ne 'print if /^PLB/' file.x12 # grep for PLB segments x12cat -ane '$amt{$F[1]} += $F[2] if /^AMT/; END{ print "$_: $amt{$_}" + for sort {$amt{$b}<=>$amt{$a}} keys %amt }' file1.x12 file2.x12 ... +# print sorted break down, by amount descending, of AMT code-types
    Now if that's not some practical extraction and reporting... I don't know what is! (If you knew what the X12 data language was, you'd think that was pretty damn cool, I guarantee it).
    ------------ :Wq Not an editor command: Wq
Re: Favourite One-liners?
by PodMaster (Abbot) on Jun 28, 2005 at 00:28 UTC
    I always use
    perl -lne"// and print "
    though it's not a favorite.

    On a side note, I'd write that (perl -e 'print <>' filename) as perl -pe1 filename.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Favourite One-liners?
by Tanktalus (Canon) on Jun 28, 2005 at 01:35 UTC
    perl -pi~ -e 's/foo/bar/' *.xyz

    I often have to go through a list of files and make minor tweaks to them. And we're talking about 150-200 files. It blows my coworkers' minds away that I can do search&replace that quickly.

    IIRC, this is actually what got me started on perl in the first place. I am waaaay too lazy.

Re: Favourite One-liners?
by sh1tn (Priest) on Jun 28, 2005 at 07:03 UTC
    C:\>perl -e "grep /STRANGE or HIDDEN file/i&&-f&&unlink, glob '*'"


Re: Favourite One-liners?
by holli (Monsignor) on Jun 28, 2005 at 07:10 UTC
    To rebuild Active State HTML-docs when I installed something from CPAN:
    perl -MActivePerl::DocTools -e "UpdateHTML(1)"
    Delayed execution of a command:
    perl -e "sleep(3600); `something.exe`"
    Nothing fancy. Just useful.


    holli, /regexed monk/
      Why not justsleep 3600; something.exe? Does that not work in Windows?

      On Unix, I generally use ( sleep 60m; xterm -bg red ) & to set a timer to pop up a big red window after an hour, without tying up the shell.

      Some of my favorites:

      Scan a log file generated by strace -tt, looking for each second turnover: (should statistically find the operations requiring the most time)

      perl -lane 'print $last if ($F[0] ne $lastt); $last = $_; $lastt = $F[ +0];'
      Print out a running sum of the 3rd column of a file:
      perl -lane 'print $s += $F[2]'
      Rewrite all of the CVS/Root files in a tree (yes, this is a weird way to do it):
      find . -name 'Root' -print0 | xargs -0 perl -i -pe '$_=q(:pserver:sfin +k@cvs/usr/local/cvs)'
      Print out all the double-quoted strings in a file:
      perl -lne 'print $1 while (/\"(.*?)\"/g)' filename
      Somewhat more robust version (handles backslashes):
      perl -lne 'print $1 while (/\"((\\.|[^\"])*)\"/g)' filename
      Print out all of the C++ constructors in a set of files:
      perl -lne 'print if /(\w+)::\1/ .. /^\}/' *.cpp
      I don't know if those are really representative of my favorites. I normally just spew them out on demand, so it's difficult to think of them when I don't actually need them. The above are just the ones I've used recently enough to remember.
Re: Favourite One-liners?
by calin (Deacon) on Jun 28, 2005 at 09:17 UTC

    I always use Perl when I need to do some calculation.

    $ perl -le 'print <arithmetic expression here>'

    The debugger is handy for iterative calculations:

    $ perl -de0

    Update - tip: Be careful with constructs like:

    print (5 + 4) * 3;

    This is equivalent to (print 5 + 4) * 3, probably not what you want.

      My most common one-liner, which accomplishes the "arithmetic evaluation" goal, is perl -ple'$_="> ".eval' (Déjà vu?)

        Other than the inherent awesomeness of producing a Perl REPL in under 25 characters, I don't see why one would prefer this one-liner to the even shorter

        perl -de1
        True, with the debugger one has to suffer the aggravation of having to prepend p or x to expressions if one wants to see the evaluated output, but in return one gets plenty of features like (off the top of my head):
        1. Readline with history (this one alone saves so much typing that it more than compensates for having to type those extra p's and x's);
        2. The option of paged output;
        3. The Joy of x, including not only pretty printing of complex data structures (with optional paging), but also the ability to specify the depth the pretty-printing;


        the lowliest monk

Re: Favourite One-liners?
by pelagic (Curate) on Jun 28, 2005 at 10:35 UTC
    # debug regex evaluation perl -e 'use re "debug"; "abc" =~ /a*b+c/;' # show path; 1 dir per line perl -e 'print join("\n", split /:/, $ENV{PATH}), "\n"' # Version of installed Module perl -MXML::Parser -e 'print "$XML::Parser::VERSION\n"'

    pelagic
Re: Favourite One-liners?
by zentara (Archbishop) on Jun 28, 2005 at 10:52 UTC
    Considering the great utility and popularity of Perl 1-liners, maybe it should have it's own section in the Code Catacombs?

    I'm not really a human, but I play one on earth. flash japh

      I was thinking of that too. Maybe a section in CUFP?

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!
Re: Favourite One-liners?
by kelan (Deacon) on Jun 28, 2005 at 12:30 UTC

    Probably the only one-liner I use often enough to get a "favorite" slot:

    perl -MModule::Name -e1
    Just checks that the module can be loaded. It's nice for quick sanity checks.

Re: Favourite One-liners?
by jfroebe (Vicar) on Jun 28, 2005 at 12:38 UTC

    Hi,

    While I do enjoy this topic, perhaps it would be helpful to new people if the one-liners have a description instead of just:
    perl .....

    Jason L. Froebe

    Team Sybase member

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Re: Favourite One-liners?
by Limbic~Region (Chancellor) on Jun 28, 2005 at 12:39 UTC
    ghenry,
    I almost never use 1 liners. I know - blasphemy huh. I do however test things from the command line rather frequently. Often questions come up in #perl that are answered with a 1 liner. Now if the question had been about sed or awk.....

    Cheers - L~R

Re: Favourite One-liners?
by tlm (Prior) on Jun 28, 2005 at 12:54 UTC

    Though it is not the infallible oracle I once thought it was, I still make frequent use of

    % perl -MO=Deparse,-p -e '<Perl code>'
    ...to figure out how perl is parsing something, or maybe to get a grip on some obfu. E.g. if you wonder what this one-liner does
    -nle '}{print$.' foobar.txt
    feed it to -MO=Deparse,-p (make sure to retain any other command line switches, like -nl here):
    % perl -MO=Deparse,-p -nle '}{print$.' BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined(($_ = <ARGV>))) { chomp($_); } { print($.); } -e syntax OK
    Note that the -p in this one-liner is part not perl's command line switch, but rather an argument to B::Deparse.

    Less commonly , I use other backends, such as B::Terse and B::Concise, instead of B::Deparse. See also O and B.

    the lowliest monk

Re: Favourite One-liners?
by Juerd (Abbot) on Jun 28, 2005 at 16:38 UTC

    There are many useful oneliners, but this one is by far my favourite:

    perl -ple'$_=eval'
    It's a GREAT calculator.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: Favourite One-liners?
by tlm (Prior) on Jun 28, 2005 at 19:39 UTC

    One-liners are an inexhaustible source of fascination. Some are so surprisingly powerful that I'm reminded of the line "the universe in a grain of sand...".

    And, predictably, this power to fascinate has also made them the object of high scorn.

    Anyway, as proof of this fascination, here's another relatively recent thread on the same subject; in particular, see further links in this node from the same thread.

    the lowliest monk

Re: Favourite One-liners?
by radiantmatrix (Parson) on Jun 28, 2005 at 21:09 UTC
    I occasionally like to remove whole-line comments from config files or Perl scripts (esp. when the comments prevent me from seeing the code clearly). This works great:
    perl -pe 'next if /^(\s*)#/' file.conf > clean-file.conf"
    Remove the (\s*) if your definition of "whole-line comment" doesn't include whitespace before the '#'. Also note that I deliberately avoid in-place edit, as the point is to make a clean copy.

    On Windows, I often like to backup generated files before doing a second test run. Since I want to compare both sets of output, sometimes I need to leave the extension intact:

    perl -e "for(glob '*.xls'){$o=$_; s/(\.xls)$/.old$1/i;rename $o,$_}"
    works smashingly (specifically for XLS files, that one).

    And finally, also on Windows (where find means something different than on UNIX), I sometimes want to remove all files in a directory (not the whole tree) that are more than 48 hours old:

    perl -e "for(glob '*'){next unless -f;@s=stat;unlink($_) if (time-$s[9 +])>60*60*48;}"
    Update: And to search the whole tree:
    perl -MFile::Find -e "find(sub{@s=stat;unlink if (time-$s[9])>60*60*48 + && -f},'.')";

    Yoda would agree with Perl design: there is no try{}

Re: Favourite One-liners?
by samtregar (Abbot) on Jun 28, 2005 at 22:41 UTC
    What could be taking so damn long:

    $ perl -d:DProf slowass.cgi 'rm=paint&value=dry' $ dprofpp -r

    Ah, it's the database. Let's see what the story is:

    $ DBI_PROFILE=DBI::ProfileDumper perl slowass.cgi 'rm=paint&value=d +ry' $ dbiprof

    -sam

Re: Favourite One-liners?
by tlm (Prior) on Jun 29, 2005 at 13:07 UTC

    I forgot this one-liner, probably because I use it so much it has become almost instinctive:

    % perl -cw script.pl
    All it does is check for compiler errors and warnings, without running the program.

    Why not just run the program? If script.pl doesn't compile, I suppose there is not much difference. Otherwise, it's a matter of "defensive programming." The check above takes one second to perform, and it can alert me to bugs that I may want to know of before ever running the program. Besides, there are times (e.g. when the coding is not yet complete) when all I want is to confirm that my code passes this first test of soundness, but not yet run the code. With a reasonably civilized editor, one can bind this capability to some convenient keyboard shortcut, so one can do the check periodically as one codes, which helps narrow down the location of errors.

    the lowliest monk

Re: Favourite One-liners?
by gellyfish (Monsignor) on Jun 29, 2005 at 13:57 UTC

    To be honest I rarely find myself doing a "pure perl" one liner more sophisticated than the occasional perl -pi -e's/something/somethingelse/g' *, however every once in a while something a little more complicated comes up and you know you've got to use different bits of the toolkit:

    perl -MEmail::Folder -e'print $_,"\n" foreach sort map { $_->header("F +rom")} Email::Folder->new("mail/london_pm")->messages()' | uniq -c | +sort -n
    Now I'm sure you could do this entirely as purely perl, but what would be the point (apart from proving that you can do it)? As it is the entire pipeline could be made shorter by piping through an additional sort before the uniq rather sorting in Perl (you can lose the map as well as the sort then). This is after all the point of having pipelines and filters and having a rich toolset.

    /J\

Re: Favourite One-liners?
by tilly (Archbishop) on Jun 30, 2005 at 01:13 UTC
    Here is my favorite off-colour one-liner. Please don't click to see it if you think you might be offended, because you probably will.
    perl -penis
    Figuring out what this does is half the fun. Yes, it does relate to pussy.
Re: Favourite One-liners?
by fizbin (Chaplain) on Jun 30, 2005 at 10:02 UTC
    There's this, which is useful in breaking up a certain type of fixed-length-record file (record size: 13612) into a file with line feeds at appropriate places:

    perl -ple 'BEGIN {$/=\13612;}' pstann.new > pstann.new.linefeed

    We can't just use the unix "pr" utility because that tends to break on upper-128 characters.

    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
Re: Favourite One-liners?
by bageler (Hermit) on Jul 01, 2005 at 21:29 UTC
    for checking if a module is installed:
    perl -MModule::Name -eprint
Re: Favourite One-liners?
by Anonymous Monk on Jul 04, 2005 at 13:45 UTC
    I always use:
    perl -e 'print <>' filename
    That's a one-liner I would never use. It's inefficient, and a lot longer to type than cat filename.

    I use one-liners a lot. However, I seldomly use Perl in one-liners - when it comes to one-liners a Unix shell (with the standard Unix tools) usually beats Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://470397]
Approved by SciDude
Front-paged by etcshadow
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2014-08-27 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (241 votes), past polls