In: , ,
On: 2004 / 05 / 01 Viewed: 59449 times
Shorter URL for this post: http://ozh.in/u

Collection of Perl one liners. The Camel ownz j00 all :)

  1. # run contents of "my_file" as a program
  2. $ perl my_file
  3.  
  4. # run debugger "stand-alone"
  5. $ perl -d -e 42
  6.  
  7. # run program, but with warnings
  8. $ perl -w my_file
  9.  
  10. # run program under debugger
  11. $ perl -d my_file
  12.  
  13. # just check syntax, with warnings
  14. $ perl -wc my_file
  15.  
  16. # useful at end of "find foo -print"
  17. $ perl -nle unlink
  18.  
  19. # simplest one-liner program
  20. $ perl -e 'print "hello world! "'
  21.  
  22. # add first and penultimate columns
  23. $ perl -lane 'print $F[0] + $F[-2]'
  24.  
  25. # just lines 15 to 17
  26. $ perl -ne 'print if 15 .. 17' *.pod
  27.  
  28. # in-place edit of *.c files changing all foo to bar
  29. $ perl -p -i.bak -e 's/\bfoo&\b/bar/g' *.c
  30.  
  31. # command-line that prints the first 50 lines (cheaply)
  32. $ perl -pe 'exit if $. > 50' f1 f2 f3 ...
  33.  
  34. # delete first 10 lines
  35. $ perl -i.old -ne 'print unless 1 .. 10' foo.txt
  36.  
  37. # change all the isolated oldvar occurrences to newvar
  38. $ perl -i.old -pe 's{\boldvar\b}{newvar}g' *.[chy]
  39.  
  40. # command-line that reverses the whole file by lines
  41. $ perl -e 'print reverse <>' file1 file2 file3 ....
  42.  
  43. # find palindromes
  44. $ perl -lne 'print if $_ eq reverse' /usr/dict/words
  45.  
  46. # command-line that reverse all the bytes in a file
  47. $ perl -0777e 'print scalar reverse <>' f1 f2 f3 ...
  48.  
  49. # command-line that reverses the whole file by paragraphs
  50. $ perl -00 -e 'print reverse <>' file1 file2 file3 ....
  51.  
  52. # increment all numbers found in these files
  53. $ perl i.tiny -pe 's/(d+)/ 1 + $1 /ge' file1 file2 ....
  54.  
  55. # command-line that shows each line with its characters backwards
  56. $ perl -nle 'print scalar reverse $_' file1 file2 file3 ....
  57.  
  58. # delete all but lines beween START and END
  59. $ perl -i.old -ne 'print unless /^START$/ .. /^END$/' foo.txt
  60.  
  61. # binary edit (careful!)
  62. $ perl -i.bak -pe 's/Mozilla/Slopoke/g' /usr/local/bin/netscape
  63.  
  64. # look for dup words
  65. $ perl -0777 -ne 'print "$.: doubled $_&\n" while /\b(&\w+)\b\s+\b\1\b/gi'
  66.  
  67. # command-line that prints the last 50 lines (expensively)
  68. $ perl -e 'lines = <>; print @@lines[ $#lines .. $#lines-50' f1 f2 f3 ...

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/u

Metastuff

This entry "Perl one-liners" was posted on 01/05/2004 at 12:18 am and is tagged with , ,
Watch this discussion : Comments RSS 2.0.

2 Blablas

  1. TomBoss says:

    FirstPost
    (Au cas ou)
    :PP

  2. castet says:

    May I suggest you the 69th perl liner:

    $ perl -e 'print unless (/tag1/)||(/tag2/)' f1

    Best regards,

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar and sign for a free account
Spam: Various spam plugins may be activated. I'll put pins in a Voodoo doll if you spam me.

Read more ?