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

Hi All,

I was looking for different ways to emulate Unix commands for my poor 'ole Windows.
I came against "Perl Power Tools: The Unix Reconstruction Project" which has been very useful,
but now I want to compile a list of useful one-liners (the kind run perl -ne).
I didn't find any serious collection anywhere (including the Monastary, although, being new,
I may not yet be very adept at finding what I need here).

Does anyone know where I can find things like "counting the lines in a file", "print lines 5 thru 70
to STDOUT" or "print the second column"? I have written a couple, and could write more,
I'm just looking for some reuse.

I also think this might be a good subject to node somehow.

Thanks,
me

--- Texarkana

Replies are listed 'Best First'.
Re: perl -ne (one-liners)
by davorg (Chancellor) on May 30, 2001 at 15:27 UTC

    Well here are the three you mention in your post:

    perl -ne 'END {print $.}' filename perl -ne 'print if 5 .. 70' filename perl -lane 'print $F[1]' filename
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: perl -ne (one-liners)
by danger (Priest) on May 30, 2001 at 19:38 UTC

    The only list of one-liners I've seen is one Tom Christiansen posted to clpm several years ago --- here is his list:

    # run contents of "my_file" as a program perl my_file # run debugger "stand-alone" perl -d -e 42 # run program, but with warnings perl -w my_file # run program under debugger perl -d my_file # just check syntax, with warnings perl -wc my_file # useful at end of "find foo -print" perl -nle unlink # simplest one-liner program perl -e 'print "hello world!\n"' # add first and penultimate columns perl -lane 'print $F[0] + $F[-2]' # just lines 15 to 17 perl -ne 'print if 15 .. 17' *.pod # in-place edit of *.c files changing all foo to bar perl -p -i.bak -e 's/\bfoo\b/bar/g' *.c # command-line that prints the first 50 lines (cheaply) perl -pe 'exit if $. > 50' f1 f2 f3 ... # delete first 10 lines perl -i.old -ne 'print unless 1 .. 10' foo.txt # change all the isolated oldvar occurrences to newvar perl -i.old -pe 's{\boldvar\b}{newvar}g' *.[chy] # command-line that reverses the whole file by lines perl -e 'print reverse <>' file1 file2 file3 .... # find palindromes perl -lne 'print if $_ eq reverse' /usr/dict/words # command-line that reverse all the bytes in a file perl -0777e 'print scalar reverse <>' f1 f2 f3 ... # command-line that reverses the whole file by paragraphs perl -00 -e 'print reverse <>' file1 file2 file3 .... # increment all numbers found in these files perl i.tiny -pe 's/(\d+)/ 1 + $1 /ge' file1 file2 .... # command-line that shows each line with its characters backwards perl -nle 'print scalar reverse $_' file1 file2 file3 .... # delete all but lines beween START and END perl -i.old -ne 'print unless /^START$/ .. /^END$/' foo.txt # binary edit (careful!) perl -i.bak -pe 's/Mozilla/Slopoke/g' /usr/local/bin/netscape # look for dup words perl -0777 -ne 'print "$.: doubled $_\n" while /\b(\w+)\b\s+\b\1\b/gi' # command-line that prints the last 50 lines (expensively) perl -e '@lines = <>; print @lines[ $#lines .. $#lines-50' f1 f2 f3 .. +.

    Note, I don't find the 'dup words' (second from last) one-liner terribly useful as it stands. You might want to change the -0777 to -000 to give the paragraph (and paragraph number) containing the duplicate, or if you want an actual line number so you can jump the start of the offending paragraph then you can fiddle with this one (split across two lines just for readability):

    perl -000 -ne '$.=$a+1 and print "$.: doubled <$1> in\n$_\n" if /\b(\w+)\b\s+\b\1\b/gi;$a+=tr/\n//' file
Re: perl -ne (one-liners)
by Brovnik (Hermit) on May 30, 2001 at 15:37 UTC
    You should also check out Cygwin if you don't already have it, it provides lots of the GNU functionality ported to Windows.
    --
    Brovnik
Re: perl -ne (one-liners)
by chipmunk (Parson) on May 30, 2001 at 20:42 UTC
    There is a semi-offical (but little-known and infrequently-updated) Perl one-liners archive at POLECAT. You can submit one-liners for inclusion in this collection by mailing rjk@history.perl.org.
Re: perl -ne (one-liners)
by RhetTbull (Curate) on May 30, 2001 at 15:46 UTC
    OK, so it's not perl... but if what you want is Un*x functionality on Windows, then install Cygwin and use the standard unix toolbox. BTW, Cygwin comes with perl ;-)
    Does anyone know where I can find things like "counting the lines in a file",
    wc -l
    "print lines 5 thru 70 to STDOUT"
    sed -n '5,70p'
    or "print the second column"
    cut -f2
Re: perl -ne (one-liners)
by bwana147 (Pilgrim) on May 30, 2001 at 15:39 UTC

    I was also thinking of writing for myself a couple of "one-liner" utilities. But then, I'm wondering: what is easier/quicker/handier (btw, is handier proper English?) when you want to, e.g. "print lines 5 thru 70 to STDOUT"?:

    • Just type:
      cat whatever | perl -ne 'print if 5..70'
    • Write a perl script that does exactly that and nothing more, stuff it into some directory, and then try hard too remember how it's called, where it is and how it works, when you need it. IMHO, you'd be better off typing the whole one-liner.

    Still, a kind of Cookbook dedicated to one-liners might be helpful, not for copy'n'pasting the recipes "as is", but merely as a source of inspiration.

    My € 0.02

    --bwana147