collection of handy perl one-liner scripts

perl1line.txt 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. Useful One-Line Scripts for Perl Nov 14 2011 | version 1.04
  2. -------------------------------- ----------- ------------
  3. Compiled by Peteris Krumins (peter@catonmat.net, @pkrumins on Twitter)
  4. http://www.catonmat.net -- good coders code, great reuse
  5. Latest version of this file is always at:
  6. http://www.catonmat.net/download/perl1line.txt
  7. This file is also available in other languages:
  8. (None at the moment.)
  9. Please email me peter@catonmat.net if you wish to translate it.
  10. Perl One-Liners on Github:
  11. https://github.com/pkrumins/perl1line.txt
  12. You can send me pull requests over GitHub! I accept bug fixes,
  13. new one-liners, translations and everything else related.
  14. I am also writing "Perl One-Liners Explained" ebook that's based on
  15. this file. It explains all the one-liners here. Get it soon at:
  16. http://www.catonmat.net/blog/perl-book/
  17. These one-liners work both on UNIX systems and Windows. Most likely your
  18. UNIX system already has Perl. For Windows get the Strawberry Perl at:
  19. http://www.strawberryperl.com/
  20. Table of contents:
  21. 1. File Spacing
  22. 2. Line Numbering
  23. 3. Calculations
  24. 4. String Creation and Array Creation
  25. 5. Text Conversion and Substitution
  26. 6. Selective Printing and Deleting of Certain Lines
  27. 7. Handy Regular Expressions
  28. 8. Perl tricks
  29. FILE SPACING
  30. ------------
  31. # Double space a file
  32. perl -pe '$\="\n"'
  33. perl -pe 'BEGIN { $\="\n" }'
  34. perl -pe '$_ .= "\n"'
  35. perl -pe 's/$/\n/'
  36. # Double space a file, except the blank lines
  37. perl -pe '$_ .= "\n" unless /^$/'
  38. perl -pe '$_ .= "\n" if /\S/'
  39. # Triple space a file
  40. perl -pe '$\="\n\n"'
  41. perl -pe '$_.="\n\n"'
  42. # N-space a file
  43. perl -pe '$_.="\n"x7'
  44. # Add a blank line before every line
  45. perl -pe 's//\n/'
  46. # Remove all blank lines
  47. perl -ne 'print unless /^$/'
  48. perl -lne 'print if length'
  49. perl -ne 'print if /\S/'
  50. # Remove all consecutive blank lines, leaving just one
  51. perl -00 -pe ''
  52. perl -00pe0
  53. # Compress/expand all blank lines into N consecutive ones
  54. perl -00 -pe '$_.="\n"x4'
  55. # Fold a file so that every set of 10 lines becomes one tab-separated line
  56. perl -lpe '$\ = $. % 10 ? "\t" : "\n"'
  57. LINE NUMBERING
  58. --------------
  59. # Number all lines in a file
  60. perl -pe '$_ = "$. $_"'
  61. # Number only non-empty lines in a file
  62. perl -pe '$_ = ++$a." $_" if /./'
  63. # Number and print only non-empty lines in a file (drop empty lines)
  64. perl -ne 'print ++$a." $_" if /./'
  65. # Number all lines but print line numbers only non-empty lines
  66. perl -pe '$_ = "$. $_" if /./'
  67. # Number only lines that match a pattern, print others unmodified
  68. perl -pe '$_ = ++$a." $_" if /regex/'
  69. # Number and print only lines that match a pattern
  70. perl -ne 'print ++$a." $_" if /regex/'
  71. # Number all lines, but print line numbers only for lines that match a pattern
  72. perl -pe '$_ = "$. $_" if /regex/'
  73. # Number all lines in a file using a custom format (emulate cat -n)
  74. perl -ne 'printf "%-5d %s", $., $_'
  75. # Print the total number of lines in a file (emulate wc -l)
  76. perl -lne 'END { print $. }'
  77. perl -le 'print $n=()=<>'
  78. perl -le 'print scalar(()=<>)'
  79. perl -le 'print scalar(@foo=<>)'
  80. perl -ne '}{print $.'
  81. perl -nE '}{say $.'
  82. # Print the number of non-empty lines in a file
  83. perl -le 'print scalar(grep{/./}<>)'
  84. perl -le 'print ~~grep{/./}<>'
  85. perl -le 'print~~grep/./,<>'
  86. perl -E 'say~~grep/./,<>'
  87. # Print the number of empty lines in a file
  88. perl -lne '$a++ if /^$/; END {print $a+0}'
  89. perl -le 'print scalar(grep{/^$/}<>)'
  90. perl -le 'print ~~grep{/^$/}<>'
  91. perl -E 'say~~grep{/^$/}<>'
  92. # Print the number of lines in a file that match a pattern (emulate grep -c)
  93. perl -lne '$a++ if /regex/; END {print $a+0}'
  94. perl -nE '$a++ if /regex/; END {say $a+0}'
  95. CALCULATIONS
  96. ------------
  97. # Check if a number is a prime
  98. perl -lne '(1x$_) !~ /^1?$|^(11+?)\1+$/ && print "$_ is prime"'
  99. # Print the sum of all the fields on a line
  100. perl -MList::Util=sum -alne 'print sum @F'
  101. # Print the sum of all the fields on all lines
  102. perl -MList::Util=sum -alne 'push @S,@F; END { print sum @S }'
  103. perl -MList::Util=sum -alne '$s += sum @F; END { print $s }'
  104. # Shuffle all fields on a line
  105. perl -MList::Util=shuffle -alne 'print "@{[shuffle @F]}"'
  106. perl -MList::Util=shuffle -alne 'print join " ", shuffle @F'
  107. # Find the minimum element on a line
  108. perl -MList::Util=min -alne 'print min @F'
  109. # Find the minimum element over all the lines
  110. perl -MList::Util=min -alne '@M = (@M, @F); END { print min @M }'
  111. perl -MList::Util=min -alne '$min = min @F; $rmin = $min unless defined $rmin && $min > $rmin; END { print $rmin }'
  112. # Find the maximum element on a line
  113. perl -MList::Util=max -alne 'print max @F'
  114. # Find the maximum element over all the lines
  115. perl -MList::Util=max -alne '@M = (@M, @F); END { print max @M }'
  116. # Replace each field with its absolute value
  117. perl -alne 'print "@{[map { abs } @F]}"'
  118. # Find the total number of fields (words) on each line
  119. perl -alne 'print scalar @F'
  120. # Print the total number of fields (words) on each line followed by the line
  121. perl -alne 'print scalar @F, " $_"'
  122. # Find the total number of fields (words) on all lines
  123. perl -alne '$t += @F; END { print $t}'
  124. # Print the total number of fields that match a pattern
  125. perl -alne 'map { /regex/ && $t++ } @F; END { print $t }'
  126. perl -alne '$t += /regex/ for @F; END { print $t }'
  127. perl -alne '$t += grep /regex/, @F; END { print $t }'
  128. # Print the total number of lines that match a pattern
  129. perl -lne '/regex/ && $t++; END { print $t }'
  130. # Print the number PI to n decimal places
  131. perl -Mbignum=bpi -le 'print bpi(n)'
  132. # Print the number PI to 39 decimal places
  133. perl -Mbignum=PI -le 'print PI'
  134. # Print the number E to n decimal places
  135. perl -Mbignum=bexp -le 'print bexp(1,n+1)'
  136. # Print the number E to 39 decimal places
  137. perl -Mbignum=e -le 'print e'
  138. # Print UNIX time (seconds since Jan 1, 1970, 00:00:00 UTC)
  139. perl -le 'print time'
  140. # Print GMT (Greenwich Mean Time) and local computer time
  141. perl -le 'print scalar gmtime'
  142. perl -le 'print scalar localtime'
  143. # Print local computer time in H:M:S format
  144. perl -le 'print join ":", (localtime)[2,1,0]'
  145. # Print yesterday's date
  146. perl -MPOSIX -le '@now = localtime; $now[3] -= 1; print scalar localtime mktime @now'
  147. # Print date 14 months, 9 days and 7 seconds ago
  148. perl -MPOSIX -le '@now = localtime; $now[0] -= 7; $now[4] -= 14; $now[7] -= 9; print scalar localtime mktime @now'
  149. # Prepend timestamps to stdout (GMT, localtime)
  150. tail -f logfile | perl -ne 'print scalar gmtime," ",$_'
  151. tail -f logfile | perl -ne 'print scalar localtime," ",$_'
  152. # Calculate factorial of 5
  153. perl -MMath::BigInt -le 'print Math::BigInt->new(5)->bfac()'
  154. perl -le '$f = 1; $f *= $_ for 1..5; print $f'
  155. # Calculate greatest common divisor (GCM)
  156. perl -MMath::BigInt=bgcd -le 'print bgcd(@list_of_numbers)'
  157. # Calculate GCM of numbers 20 and 35 using Euclid's algorithm
  158. perl -le '$n = 20; $m = 35; ($m,$n) = ($n,$m%$n) while $n; print $m'
  159. # Calculate least common multiple (LCM) of numbers 35, 20 and 8
  160. perl -MMath::BigInt=blcm -le 'print blcm(35,20,8)'
  161. # Calculate LCM of 20 and 35 using Euclid's formula: n*m/gcd(n,m)
  162. perl -le '$a = $n = 20; $b = $m = 35; ($m,$n) = ($n,$m%$n) while $n; print $a*$b/$m'
  163. # Generate 10 random numbers between 5 and 15 (excluding 15)
  164. perl -le '$n=10; $min=5; $max=15; $, = " "; print map { int(rand($max-$min))+$min } 1..$n'
  165. # Find and print all permutations of a list
  166. perl -MAlgorithm::Permute -le '$l = [1,2,3,4,5]; $p = Algorithm::Permute->new($l); print @r while @r = $p->next'
  167. # Generate the power set
  168. perl -MList::PowerSet=powerset -le '@l = (1,2,3,4,5); for (@{powerset(@l)}) { print "@$_" }'
  169. # Convert an IP address to unsigned integer
  170. perl -le '$i=3; $u += ($_<<8*$i--) for "127.0.0.1" =~ /(\d+)/g; print $u'
  171. perl -le '$ip="127.0.0.1"; $ip =~ s/(\d+)\.?/sprintf("%02x", $1)/ge; print hex($ip)'
  172. perl -le 'print unpack("N", 127.0.0.1)'
  173. perl -MSocket -le 'print unpack("N", inet_aton("127.0.0.1"))'
  174. # Convert an unsigned integer to an IP address
  175. perl -MSocket -le 'print inet_ntoa(pack("N", 2130706433))'
  176. perl -le '$ip = 2130706433; print join ".", map { (($ip>>8*($_))&0xFF) } reverse 0..3'
  177. perl -le '$ip = 2130706433; $, = "."; print map { (($ip>>8*($_))&0xFF) } reverse 0..3'
  178. STRING CREATION AND ARRAY CREATION
  179. ----------------------------------
  180. # Generate and print the alphabet
  181. perl -le 'print a..z'
  182. perl -le 'print ("a".."z")'
  183. perl -le '$, = ","; print ("a".."z")'
  184. perl -le 'print join ",", ("a".."z")'
  185. # Generate and print all the strings from "a" to "zz"
  186. perl -le 'print ("a".."zz")'
  187. perl -le 'print "aa".."zz"'
  188. # Create a hex lookup table
  189. @hex = (0..9, "a".."f")
  190. # Convert a decimal number to hex using @hex lookup table
  191. perl -le '$num = 255; @hex = (0..9, "a".."f"); while ($num) { $s = $hex[($num%16)&15].$s; $num = int $num/16 } print $s'
  192. perl -le '$hex = sprintf("%x", 255); print $hex'
  193. perl -le '$num = "ff"; print hex $num'
  194. # Generate a random 8 character password
  195. perl -le 'print map { ("a".."z")[rand 26] } 1..8'
  196. perl -le 'print map { ("a".."z", 0..9)[rand 36] } 1..8'
  197. # Create a string of specific length
  198. perl -le 'print "a"x50'
  199. # Create a repeated list of elements
  200. perl -le '@list = (1,2)x20; print "@list"'
  201. # Create an array from a string
  202. @months = split ' ', "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
  203. @months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/
  204. # Create a string from an array
  205. @stuff = ("hello", 0..9, "world"); $string = join '-', @stuff
  206. # Find the numeric values for characters in the string
  207. perl -le 'print join ", ", map { ord } split //, "hello world"'
  208. # Convert a list of numeric ASCII values into a string
  209. perl -le '@ascii = (99, 111, 100, 105, 110, 103); print pack("C*", @ascii)'
  210. perl -le '@ascii = (99, 111, 100, 105, 110, 103); print map { chr } @ascii'
  211. # Generate an array with odd numbers from 1 to 100
  212. perl -le '@odd = grep {$_ % 2 == 1} 1..100; print "@odd"'
  213. perl -le '@odd = grep { $_ & 1 } 1..100; print "@odd"'
  214. # Generate an array with even numbers from 1 to 100
  215. perl -le '@even = grep {$_ % 2 == 0} 1..100; print "@even"'
  216. # Find the length of the string
  217. perl -le 'print length "one-liners are great"'
  218. # Find the number of elements in an array
  219. perl -le '@array = ("a".."z"); print scalar @array'
  220. perl -le '@array = ("a".."z"); print $#array + 1'
  221. TEXT CONVERSION AND SUBSTITUTION
  222. --------------------------------
  223. # ROT13 a string
  224. 'y/A-Za-z/N-ZA-Mn-za-m/'
  225. # ROT 13 a file
  226. perl -lpe 'y/A-Za-z/N-ZA-Mn-za-m/' file
  227. # Base64 encode a string
  228. perl -MMIME::Base64 -e 'print encode_base64("string")'
  229. perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' file
  230. # Base64 decode a string
  231. perl -MMIME::Base64 -le 'print decode_base64("base64string")'
  232. perl -MMIME::Base64 -ne 'print decode_base64($_)' file
  233. # URL-escape a string
  234. perl -MURI::Escape -le 'print uri_escape($string)'
  235. # URL-unescape a string
  236. perl -MURI::Escape -le 'print uri_unescape($string)'
  237. # HTML-encode a string
  238. perl -MHTML::Entities -le 'print encode_entities($string)'
  239. # HTML-decode a string
  240. perl -MHTML::Entities -le 'print decode_entities($string)'
  241. # Convert all text to uppercase
  242. perl -nle 'print uc'
  243. perl -ple '$_=uc'
  244. perl -nle 'print "\U$_"'
  245. # Convert all text to lowercase
  246. perl -nle 'print lc'
  247. perl -ple '$_=lc'
  248. perl -nle 'print "\L$_"'
  249. # Uppercase only the first word of each line
  250. perl -nle 'print ucfirst lc'
  251. perl -nle 'print "\u\L$_"'
  252. # Invert the letter case
  253. perl -ple 'y/A-Za-z/a-zA-Z/'
  254. # Camel case each line
  255. perl -ple 's/(\w+)/\u$1/g'
  256. perl -ple 's/(?<!['])(\w+)/\u\1/g'
  257. # Strip leading whitespace (spaces, tabs) from the beginning of each line
  258. perl -ple 's/^[ \t]+//'
  259. perl -ple 's/^\s+//'
  260. # Strip trailing whitespace (space, tabs) from the end of each line
  261. perl -ple 's/[ \t]+$//'
  262. # Strip whitespace from the beginning and end of each line
  263. perl -ple 's/^[ \t]+|[ \t]+$//g'
  264. # Convert UNIX newlines to DOS/Windows newlines
  265. perl -pe 's|\n|\r\n|'
  266. # Convert DOS/Windows newlines to UNIX newlines
  267. perl -pe 's|\r\n|\n|'
  268. # Convert UNIX newlines to Mac newlines
  269. perl -pe 's|\n|\r|'
  270. # Substitute (find and replace) "foo" with "bar" on each line
  271. perl -pe 's/foo/bar/'
  272. # Substitute (find and replace) all "foo"s with "bar" on each line
  273. perl -pe 's/foo/bar/g'
  274. # Substitute (find and replace) "foo" with "bar" on lines that match "baz"
  275. perl -pe '/baz/ && s/foo/bar/'
  276. SELECTIVE PRINTING AND DELETING OF CERTAIN LINES
  277. ------------------------------------------------
  278. # Print the first line of a file (emulate head -1)
  279. perl -ne 'print; exit'
  280. # Print the first 10 lines of a file (emulate head -10)
  281. perl -ne 'print if $. <= 10'
  282. perl -ne '$. <= 10 && print'
  283. # Print the last line of a file (emulate tail -1)
  284. perl -ne '$last = $_; END { print $last }'
  285. perl -ne 'print if eof'
  286. # Print the last 10 lines of a file (emulate tail -10)
  287. perl -ne 'push @a, $_; @a = @a[@a-10..$#a]; END { print @a }'
  288. # Print only lines that match a regular expression
  289. perl -ne '/regex/ && print'
  290. # Print only lines that do not match a regular expression
  291. perl -ne '!/regex/ && print'
  292. # Print the line before a line that matches a regular expression
  293. perl -ne '/regex/ && $last && print $last; $last = $_'
  294. # Print the line after a line that matches a regular expression
  295. perl -ne 'if ($p) { print; $p = 0 } $p++ if /regex/'
  296. # Print lines that match regex AAA and regex BBB in any order
  297. perl -ne '/AAA/ && /BBB/ && print'
  298. # Print lines that don't match match regexes AAA and BBB
  299. perl -ne '!/AAA/ && !/BBB/ && print'
  300. # Print lines that match regex AAA followed by regex BBB followed by CCC
  301. perl -ne '/AAA.*BBB.*CCC/ && print'
  302. # Print lines that are 80 chars or longer
  303. perl -ne 'print if length >= 80'
  304. # Print lines that are less than 80 chars in length
  305. perl -ne 'print if length < 80'
  306. # Print only line 13
  307. perl -ne '$. == 13 && print && exit'
  308. # Print all lines except line 27
  309. perl -ne '$. != 27 && print'
  310. perl -ne 'print if $. != 27'
  311. # Print only lines 13, 19 and 67
  312. perl -ne 'print if $. == 13 || $. == 19 || $. == 67'
  313. perl -ne 'print if int($.) ~~ (13, 19, 67)'
  314. # Print all lines between two regexes (including lines that match regex)
  315. perl -ne 'print if /regex1/../regex2/'
  316. # Print all lines from line 17 to line 30
  317. perl -ne 'print if $. >= 17 && $. <= 30'
  318. perl -ne 'print if int($.) ~~ (17..30)'
  319. perl -ne 'print if grep { $_ == $. } 17..30'
  320. # Print the longest line
  321. perl -ne '$l = $_ if length($_) > length($l); END { print $l }'
  322. # Print the shortest line
  323. perl -ne '$s = $_ if $. == 1; $s = $_ if length($_) < length($s); END { print $s }'
  324. # Print all lines that contain a number
  325. perl -ne 'print if /\d/'
  326. # Find all lines that contain only a number
  327. perl -ne 'print if /^\d+$/'
  328. # Print all lines that contain only characters
  329. perl -ne 'print if /^[[:alpha:]]+$/
  330. # Print every second line
  331. perl -ne 'print if $. % 2'
  332. # Print every second line, starting the second line
  333. perl -ne 'print if $. % 2 == 0'
  334. # Print all lines that repeat
  335. perl -ne 'print if ++$a{$_} == 2'
  336. # Print all unique lines
  337. perl -ne 'print unless $a{$_}++'
  338. HANDY REGULAR EXPRESSIONS
  339. -------------------------
  340. # Match something that looks like an IP address
  341. /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
  342. /^(\d{1,3}\.){3}\d{1,3}$/
  343. # Test if a number is in range 0-255
  344. /^([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/
  345. # Match an IP address
  346. my $ip_part = qr|([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|;
  347. if ($ip =~ /^($ip_part\.){3}$ip_part$/) {
  348. say "valid ip";
  349. }
  350. # Check if the string looks like an email address
  351. /.+@.+\..+/
  352. # Check if the string is a decimal number
  353. /^\d+$/
  354. /^[+-]?\d+$/
  355. /^[+-]?\d+\.?\d*$/
  356. # Check if the string is a hexadecimal number
  357. /^0x[0-9a-f]+$/i
  358. # Check if the string is an octal number
  359. /^0[0-7]+$/
  360. # Check if the string is binary
  361. /^[01]+$/
  362. # Check if a word appears twice in the string
  363. /(word).*\1/
  364. # Increase all numbers by one in the string
  365. $str =~ s/(\d+)/$1+1/ge
  366. # Extract HTTP User-Agent string from the HTTP headers
  367. /^User-Agent: (.+)$/
  368. # Match printable ASCII characters
  369. /[ -~]/
  370. # Match unprintable ASCII characters
  371. /[^ -~]/
  372. # Match text between two HTML tags
  373. m|<strong>([^<]*)</strong>|
  374. m|<strong>(.*?)</strong>|
  375. # Replace all <b> tags with <strong>
  376. $html =~ s|<(/)?b>|<$1strong>|g
  377. # Extract all matches from a regular expression
  378. my @matches = $text =~ /regex/g;
  379. PERL TRICKS
  380. -----------
  381. # Print the version of a Perl module
  382. perl -MModule -le 'print $Module::VERSION'
  383. perl -MLWP::UserAgent -le 'print $LWP::UserAgent::VERSION'
  384. PERL ONE-LINERS EXPLAINED E-BOOK
  385. --------------------------------
  386. I am writing an ebook based on the one-liners in this file. If you wish to
  387. support my work and learn more about these one-liners, you can get a copy
  388. of my ebook soon at:
  389. http://www.catonmat.net/blog/perl-book/
  390. The ebook is based on the 7-part article series that I wrote on my blog.
  391. In the ebook I reviewed all the one-liners, improved explanations and added
  392. new ones.
  393. You can read the original article series here:
  394. http://www.catonmat.net/blog/perl-one-liners-explained-part-one/
  395. http://www.catonmat.net/blog/perl-one-liners-explained-part-two/
  396. http://www.catonmat.net/blog/perl-one-liners-explained-part-three/
  397. http://www.catonmat.net/blog/perl-one-liners-explained-part-four/
  398. http://www.catonmat.net/blog/perl-one-liners-explained-part-five/
  399. http://www.catonmat.net/blog/perl-one-liners-explained-part-six/
  400. http://www.catonmat.net/blog/perl-one-liners-explained-part-seven/
  401. CREDITS
  402. -------
  403. Andy Lester http://www.petdance.com
  404. Shlomi Fish http://www.shlomifish.org
  405. Madars Virza http://www.madars.org
  406. caffecaldo https://github.com/caffecaldo
  407. Kirk Kimmel https://github.com/kimmel
  408. FOUND A BUG? HAVE ANOTHER ONE-LINER?
  409. ------------------------------------
  410. Email bugs and new one-liners to me at peter@catonmat.net!
  411. HAVE FUN
  412. --------
  413. I hope you found these one-liners useful. Have fun!
  414. #---end of file---