In: , ,
On: 2004 / 05 / 01 Viewed: 33229 times

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

PERL:
  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 ...

Related posts

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. You can trackback this post from your own site

No Comment yet

  1. 1
    TomBoss France »
    said, on 04/May/04 at 12:13 am # :

    FirstPost
    (Au cas ou)
    :PP

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?

Close
E-mail It