Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: The problem with one-liners...

by jmcnamara (Monsignor)
on Feb 03, 2003 at 18:00 UTC ( #232332=note: print w/replies, xml ) Need Help??

in reply to The problem with one-liners...

That's why I keep a file of one-liners and here it is:

# Deparse something perl -MO=Deparse -e '' # wc -l # Clarified as per node 232599 perl -le 'print $==()=<>' file # Time to the 1000000000 rollover perl -le 'print reverse(1..9)-time' perl -le 'print 1e9-time' # Count the number of lines in a file perl -lpe'}{$_=$.' /etc/passwd # Sum the numbers in the first column of a file perl -lpe '$,+=$_}{$_=+$,' file perl -lpe '${}}+=+${_}}{${_}+=+${}}' numfile # Remove the first column perl -nale 'shift@F;print"@F"' # Get config info perl -MConfig -le 'print Config::myconfig()' perl -MConfig -le 'print $Config{startperl}' perl -MConfig=myconfig -le 'print myconfig()' perl -V:startperl # Find the version number of a module perl -le 'eval "require $ARGV[0]" and print $ARGV[0]->VERSION' Som +e::Module # Check if a module exists perl -le 'print 0 + eval "require $ARGV[0]" ' Some::Module # dos2unix perl -i -pe 's/\r//' dos.txt # JAAH perl -le 'use Inline AWK=>q+function j(){print"Just Another Awk Ha +cker"}+;j' # wc -l perl -MInline=awk,END{NR} -le 'print awk()' file # Create a slice list perl -le '@a = (0,1,2,3); @b = map {$_,$_+@a} @a; print "@b"' perl -le '@a = (0,1,2,3); @b = map {$_,$_+@a} (0..$#a); print "@b" +' # Weave 2 arrays perl -le '@a=(1,2);@b=('a','b'); @c = (@a,@b)[map {$_,$_+@a} (0..$ +#a)]; print "@c"' # Doublespace a file perl -pe '$_.=$/' file perl -00lpe 's/\n/\n\n/g' file perl -pe '$_=$/.$_ if /\S/' file.txt | cat -e # Print every line twice perl -pe '$\=$_' file # Ghost in the machine. perl -MCPAN -e 'install jmcnamara & _ x ord $ ;' | tail -1 # Last line of a file perl -pe '$*=$_}{$_=$*' file perl -e 'print+(<ARGV>)[-1]' file # tail perl -ne '$i=$.%6; $a[$i]=$_; END{print @a[$i+1..$#a,0..$i]}' file perl -ne 'END{print@a[$i+1..$#a,0..$i]}$a[$i=$.%5]=$_' file perl -ne 'END{print@a[$i+1..@a,0..$i]}$a[$i=$.%5]=$_' file perl -pe '$a[$i=$.%5]=$_}{print@a[$i+1..@a,0..$i]' file perl -pe '$_[$==$.%5]=$_}{print@_[$=+1..@_,0..$=]' file # -s perl -sle 'print $#' -- -#=3 # Print filename before line perl -pe '$_="$ARGV: $_"' file # Print filename and line number before line perl -ne 'print $ARGV,":", $., ": ", $_; $. = 0 if eof' file file2 # extract pod perl -nle 'print if /^=/ .. /^=cut/' file # extract code perl -nle 'print if not /^=/ .. /^=cut/' file # print print japh perl -e '$#="Just Another Perl Hacker$/"; print print' # print a line of a file perl -ne 'print and exit if $. == 3' file perl -ne 'print and last if $. == 3' file # grep -v perl -ne 'print if not /foo/' file perl -pe 'goto LINE if /foo/' file # Generate a random word perl -le 'print map{(a..z)[rand 26]} 0..rand 10' # print lines with only one word perl -nae 'print if @F == 1' file # Remove repeated lines perl -i.bak -ne 'print unless $h{$_}++' file sort -o thefile -u file # merlyn # remove all occurance of pattern except for the first one perl -ne 'print unless /pattern/ and $i++' file # Generate a list of months cal 2002 | perl -lane '@F==3&&print for@F' cal 2002 | perl -lane '@F==3&&print substr $_, 0, 3 for @F' # remove blank lines perl -ane '@F&&print' file perl -lp00ee file # abigail perl -llpp00ee file # abigail perl -l -00 -perl file # merlyn # BCD encode perl -le 'print join " ", map {ord pack "H2", $_} shift =~ /../g' +num # BCD decode perl -le 'print unpack "H*", pack "C*", @ARGV' num1 num2 ... perl -le 'print map {int($_/16).($_%16)} @ARGV' num1 num2 ... # Add newline to the end of a file perl -plee file1 file2 ... perl -pe '$_ .= $/ if eof and not /\n/' file1 file2 ... perl -i -pl -end file # Check a file for tabs in a file perl -ne 'die "File contains tabs.\n" if /\t/' file perl -ne 'die "File $ARGV contains tabs\n" if /\t/' file # Print a match and exit perl -ne 'print and last if /foo/' file # Exit after first match perl -ne 'print; last if /foo/' file # Exit before first match perl -pe 'last if /foo/' file perl -pe 'die $_ if /foo/' file # Count the vowels in a file perl -lne 'END{print $c} $c += tr/aeiou//' file


Replies are listed 'Best First'.
Re: Re: The problem with one-liners...
by petral (Curate) on Feb 03, 2003 at 23:40 UTC
    Oh well, a little fiddling since it's such fun stuff:
    # Last line of a file perl -pe '$*=$_}{$_=$*' file
    ->   perl -pe '$\=$_}{' file

    Which leads me to a head-tail (useful eg, for finding out the range of a log file).
    perl -pe '$*||=$_; $\=$_}{$_=$*' file
    and (not stopping while he's ahead) . . .
    # Exit after first match perl -ne 'print; last if /foo/' file
    ->   perl -pe '/foo/...last' file

Re: Re: The problem with one-liners...
by perrin (Chancellor) on Feb 03, 2003 at 19:04 UTC
    That's a nice collection. It would be cool if people would post some of their favorite one-liners to the snippets section.
Re: Re: The problem with one-liners...
by jmcnamara (Monsignor) on Feb 05, 2003 at 11:11 UTC

    In relation to this node I'd like to point out that these one-liners aren't always the ones that I use to accomplish a task. For example there are several head(1) and tail(1) variations. However, I never use these, I always use the system commands instead.

    In addition it doesn't contain all of the one-liners that I ever use. There are many of the variety "print all lines greater than 72 characters" or "print the third field of every line" that I can easily reproduce and which I don't have to record.

    Instead this list contains some longer one-liners that I might not remember, such as the module version one, and a lot of quirky variations that might be useful in golf or elsewhere. As such it serves me more as a sketchbook of ideas for future work than as a completed reference.


Re: Re: The problem with one-liners...
by t'mo (Pilgrim) on Feb 04, 2003 at 18:41 UTC

    I think the "random word" is clever. I was also going to suggest something like:

    perl -e 'require Some::Module;'

    (golf, anyone?) for checking for the existence of some module, but then I realized that the intent of yours was probably to produce a "boolean" value, which could be more useful to something like an install script than mine, which will either produce nothing or some text.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://232332]
and !@monks...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2017-09-20 08:10 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (234 votes). Check out past polls.