Skip to content
Newer
Older
100755 614 lines (425 sloc) 17.9 KB
638eeea @pkrumins add a link to no starch press
authored Dec 3, 2013
1 Useful One-Line Scripts for Perl Dec 03 2013 | version 1.10
3707f97 @pkrumins version 1.01 now
authored Nov 14, 2011
2 -------------------------------- ----------- ------------
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
3
4 Compiled by Peteris Krumins (peter@catonmat.net, @pkrumins on Twitter)
5 http://www.catonmat.net -- good coders code, great reuse
6
7 Latest version of this file is always at:
8
9 http://www.catonmat.net/download/perl1line.txt
10
11 This file is also available in other languages:
06d79ca @pkrumins add chinese translation
authored Mar 15, 2012
12
13 Chinese: https://github.com/vinian/perl1line.txt
14
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
15 Please email me peter@catonmat.net if you wish to translate it.
16
4c716f5 @pkrumins add github link
authored Nov 17, 2011
17 Perl One-Liners on Github:
e752f32 @pkrumins Perl One-Liners Explained e-book is out!
authored Jan 28, 2012
18
4c716f5 @pkrumins add github link
authored Nov 17, 2011
19 https://github.com/pkrumins/perl1line.txt
e752f32 @pkrumins Perl One-Liners Explained e-book is out!
authored Jan 28, 2012
20
4c716f5 @pkrumins add github link
authored Nov 17, 2011
21 You can send me pull requests over GitHub! I accept bug fixes,
22 new one-liners, translations and everything else related.
23
e752f32 @pkrumins Perl One-Liners Explained e-book is out!
authored Jan 28, 2012
24 I have also written "Perl One-Liners Explained" ebook that's based on
25 this file. It explains all the one-liners here. Get it at:
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
26
27 http://www.catonmat.net/blog/perl-book/
28
638eeea @pkrumins add a link to no starch press
authored Dec 3, 2013
29 No Starch Press has published "Perl One-Liners" as a real book too:
30
31 http://nostarch.com/perloneliners
32
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
33 These one-liners work both on UNIX systems and Windows. Most likely your
34 UNIX system already has Perl. For Windows get the Strawberry Perl at:
35
36 http://www.strawberryperl.com/
37
38 Table of contents:
39
40 1. File Spacing
41 2. Line Numbering
42 3. Calculations
43 4. String Creation and Array Creation
44 5. Text Conversion and Substitution
45 6. Selective Printing and Deleting of Certain Lines
46 7. Handy Regular Expressions
a820a8a @pkrumins added perl tricks section. added print the version of a perl module
authored Nov 14, 2011
47 8. Perl tricks
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
48
49
50 FILE SPACING
51 ------------
52
53 # Double space a file
54 perl -pe '$\="\n"'
55 perl -pe 'BEGIN { $\="\n" }'
56 perl -pe '$_ .= "\n"'
57 perl -pe 's/$/\n/'
638eeea @pkrumins add a link to no starch press
authored Dec 3, 2013
58 perl -nE 'say'
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
59
60 # Double space a file, except the blank lines
61 perl -pe '$_ .= "\n" unless /^$/'
62 perl -pe '$_ .= "\n" if /\S/'
63
64 # Triple space a file
65 perl -pe '$\="\n\n"'
66 perl -pe '$_.="\n\n"'
67
68 # N-space a file
69 perl -pe '$_.="\n"x7'
70
71 # Add a blank line before every line
72 perl -pe 's//\n/'
73
74 # Remove all blank lines
75 perl -ne 'print unless /^$/'
76 perl -lne 'print if length'
77 perl -ne 'print if /\S/'
78
79 # Remove all consecutive blank lines, leaving just one
80 perl -00 -pe ''
81 perl -00pe0
82
83 # Compress/expand all blank lines into N consecutive ones
84 perl -00 -pe '$_.="\n"x4'
85
a793776 @pkrumins v1.02 now
authored Nov 15, 2011
86 # Fold a file so that every set of 10 lines becomes one tab-separated line
a04cd47 Add line-folding one-liner
Nic Wolff authored Nov 14, 2011
87 perl -lpe '$\ = $. % 10 ? "\t" : "\n"'
88
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
89
90 LINE NUMBERING
91 --------------
92
93 # Number all lines in a file
94 perl -pe '$_ = "$. $_"'
95
96 # Number only non-empty lines in a file
97 perl -pe '$_ = ++$a." $_" if /./'
98
99 # Number and print only non-empty lines in a file (drop empty lines)
100 perl -ne 'print ++$a." $_" if /./'
101
102 # Number all lines but print line numbers only non-empty lines
103 perl -pe '$_ = "$. $_" if /./'
104
105 # Number only lines that match a pattern, print others unmodified
106 perl -pe '$_ = ++$a." $_" if /regex/'
107
108 # Number and print only lines that match a pattern
109 perl -ne 'print ++$a." $_" if /regex/'
110
111 # Number all lines, but print line numbers only for lines that match a pattern
112 perl -pe '$_ = "$. $_" if /regex/'
113
114 # Number all lines in a file using a custom format (emulate cat -n)
115 perl -ne 'printf "%-5d %s", $., $_'
116
117 # Print the total number of lines in a file (emulate wc -l)
118 perl -lne 'END { print $. }'
119 perl -le 'print $n=()=<>'
120 perl -le 'print scalar(()=<>)'
121 perl -le 'print scalar(@foo=<>)'
122 perl -ne '}{print $.'
7ab0ea6 @kimmel A few additions using Perl 5.10 for shorter commands.
kimmel authored Nov 16, 2011
123 perl -nE '}{say $.'
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
124
125 # Print the number of non-empty lines in a file
126 perl -le 'print scalar(grep{/./}<>)'
127 perl -le 'print ~~grep{/./}<>'
128 perl -le 'print~~grep/./,<>'
7ab0ea6 @kimmel A few additions using Perl 5.10 for shorter commands.
kimmel authored Nov 16, 2011
129 perl -E 'say~~grep/./,<>'
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
130
131 # Print the number of empty lines in a file
132 perl -lne '$a++ if /^$/; END {print $a+0}'
133 perl -le 'print scalar(grep{/^$/}<>)'
134 perl -le 'print ~~grep{/^$/}<>'
7ab0ea6 @kimmel A few additions using Perl 5.10 for shorter commands.
kimmel authored Nov 16, 2011
135 perl -E 'say~~grep{/^$/}<>'
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
136
137 # Print the number of lines in a file that match a pattern (emulate grep -c)
138 perl -lne '$a++ if /regex/; END {print $a+0}'
7ab0ea6 @kimmel A few additions using Perl 5.10 for shorter commands.
kimmel authored Nov 16, 2011
139 perl -nE '$a++ if /regex/; END {say $a+0}'
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
140
141
142 CALCULATIONS
143 ------------
144
145 # Check if a number is a prime
146 perl -lne '(1x$_) !~ /^1?$|^(11+?)\1+$/ && print "$_ is prime"'
147
148 # Print the sum of all the fields on a line
149 perl -MList::Util=sum -alne 'print sum @F'
150
151 # Print the sum of all the fields on all lines
152 perl -MList::Util=sum -alne 'push @S,@F; END { print sum @S }'
153 perl -MList::Util=sum -alne '$s += sum @F; END { print $s }'
154
155 # Shuffle all fields on a line
156 perl -MList::Util=shuffle -alne 'print "@{[shuffle @F]}"'
157 perl -MList::Util=shuffle -alne 'print join " ", shuffle @F'
158
159 # Find the minimum element on a line
160 perl -MList::Util=min -alne 'print min @F'
161
162 # Find the minimum element over all the lines
163 perl -MList::Util=min -alne '@M = (@M, @F); END { print min @M }'
164 perl -MList::Util=min -alne '$min = min @F; $rmin = $min unless defined $rmin && $min > $rmin; END { print $rmin }'
165
166 # Find the maximum element on a line
167 perl -MList::Util=max -alne 'print max @F'
168
169 # Find the maximum element over all the lines
170 perl -MList::Util=max -alne '@M = (@M, @F); END { print max @M }'
171
172 # Replace each field with its absolute value
173 perl -alne 'print "@{[map { abs } @F]}"'
174
175 # Find the total number of fields (words) on each line
176 perl -alne 'print scalar @F'
177
178 # Print the total number of fields (words) on each line followed by the line
179 perl -alne 'print scalar @F, " $_"'
180
181 # Find the total number of fields (words) on all lines
182 perl -alne '$t += @F; END { print $t}'
183
184 # Print the total number of fields that match a pattern
185 perl -alne 'map { /regex/ && $t++ } @F; END { print $t }'
186 perl -alne '$t += /regex/ for @F; END { print $t }'
187 perl -alne '$t += grep /regex/, @F; END { print $t }'
188
189 # Print the total number of lines that match a pattern
190 perl -lne '/regex/ && $t++; END { print $t }'
191
192 # Print the number PI to n decimal places
193 perl -Mbignum=bpi -le 'print bpi(n)'
194
195 # Print the number PI to 39 decimal places
196 perl -Mbignum=PI -le 'print PI'
197
198 # Print the number E to n decimal places
199 perl -Mbignum=bexp -le 'print bexp(1,n+1)'
200
201 # Print the number E to 39 decimal places
202 perl -Mbignum=e -le 'print e'
203
204 # Print UNIX time (seconds since Jan 1, 1970, 00:00:00 UTC)
205 perl -le 'print time'
206
207 # Print GMT (Greenwich Mean Time) and local computer time
208 perl -le 'print scalar gmtime'
209 perl -le 'print scalar localtime'
210
211 # Print local computer time in H:M:S format
212 perl -le 'print join ":", (localtime)[2,1,0]'
213
214 # Print yesterday's date
215 perl -MPOSIX -le '@now = localtime; $now[3] -= 1; print scalar localtime mktime @now'
216
217 # Print date 14 months, 9 days and 7 seconds ago
218 perl -MPOSIX -le '@now = localtime; $now[0] -= 7; $now[4] -= 14; $now[7] -= 9; print scalar localtime mktime @now'
219
1688e15 @caffecaldo Added "prepend timestamps" examples.
caffecaldo authored Nov 15, 2011
220 # Prepend timestamps to stdout (GMT, localtime)
221 tail -f logfile | perl -ne 'print scalar gmtime," ",$_'
222 tail -f logfile | perl -ne 'print scalar localtime," ",$_'
223
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
224 # Calculate factorial of 5
225 perl -MMath::BigInt -le 'print Math::BigInt->new(5)->bfac()'
226 perl -le '$f = 1; $f *= $_ for 1..5; print $f'
227
228 # Calculate greatest common divisor (GCM)
229 perl -MMath::BigInt=bgcd -le 'print bgcd(@list_of_numbers)'
230
231 # Calculate GCM of numbers 20 and 35 using Euclid's algorithm
232 perl -le '$n = 20; $m = 35; ($m,$n) = ($n,$m%$n) while $n; print $m'
233
234 # Calculate least common multiple (LCM) of numbers 35, 20 and 8
235 perl -MMath::BigInt=blcm -le 'print blcm(35,20,8)'
236
237 # Calculate LCM of 20 and 35 using Euclid's formula: n*m/gcd(n,m)
238 perl -le '$a = $n = 20; $b = $m = 35; ($m,$n) = ($n,$m%$n) while $n; print $a*$b/$m'
239
240 # Generate 10 random numbers between 5 and 15 (excluding 15)
241 perl -le '$n=10; $min=5; $max=15; $, = " "; print map { int(rand($max-$min))+$min } 1..$n'
242
243 # Find and print all permutations of a list
244 perl -MAlgorithm::Permute -le '$l = [1,2,3,4,5]; $p = Algorithm::Permute->new($l); print @r while @r = $p->next'
245
246 # Generate the power set
247 perl -MList::PowerSet=powerset -le '@l = (1,2,3,4,5); for (@{powerset(@l)}) { print "@$_" }'
248
249 # Convert an IP address to unsigned integer
250 perl -le '$i=3; $u += ($_<<8*$i--) for "127.0.0.1" =~ /(\d+)/g; print $u'
251 perl -le '$ip="127.0.0.1"; $ip =~ s/(\d+)\.?/sprintf("%02x", $1)/ge; print hex($ip)'
252 perl -le 'print unpack("N", 127.0.0.1)'
253 perl -MSocket -le 'print unpack("N", inet_aton("127.0.0.1"))'
254
255 # Convert an unsigned integer to an IP address
256 perl -MSocket -le 'print inet_ntoa(pack("N", 2130706433))'
257 perl -le '$ip = 2130706433; print join ".", map { (($ip>>8*($_))&0xFF) } reverse 0..3'
258 perl -le '$ip = 2130706433; $, = "."; print map { (($ip>>8*($_))&0xFF) } reverse 0..3'
259
260
261 STRING CREATION AND ARRAY CREATION
262 ----------------------------------
263
264 # Generate and print the alphabet
265 perl -le 'print a..z'
266 perl -le 'print ("a".."z")'
267 perl -le '$, = ","; print ("a".."z")'
268 perl -le 'print join ",", ("a".."z")'
269
270 # Generate and print all the strings from "a" to "zz"
271 perl -le 'print ("a".."zz")'
272 perl -le 'print "aa".."zz"'
273
274 # Create a hex lookup table
275 @hex = (0..9, "a".."f")
276
277 # Convert a decimal number to hex using @hex lookup table
278 perl -le '$num = 255; @hex = (0..9, "a".."f"); while ($num) { $s = $hex[($num%16)&15].$s; $num = int $num/16 } print $s'
279 perl -le '$hex = sprintf("%x", 255); print $hex'
280 perl -le '$num = "ff"; print hex $num'
281
282 # Generate a random 8 character password
283 perl -le 'print map { ("a".."z")[rand 26] } 1..8'
284 perl -le 'print map { ("a".."z", 0..9)[rand 36] } 1..8'
285
286 # Create a string of specific length
287 perl -le 'print "a"x50'
288
289 # Create a repeated list of elements
290 perl -le '@list = (1,2)x20; print "@list"'
291
292 # Create an array from a string
293 @months = split ' ', "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
294 @months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/
295
296 # Create a string from an array
297 @stuff = ("hello", 0..9, "world"); $string = join '-', @stuff
298
299 # Find the numeric values for characters in the string
300 perl -le 'print join ", ", map { ord } split //, "hello world"'
301
302 # Convert a list of numeric ASCII values into a string
303 perl -le '@ascii = (99, 111, 100, 105, 110, 103); print pack("C*", @ascii)'
304 perl -le '@ascii = (99, 111, 100, 105, 110, 103); print map { chr } @ascii'
305
306 # Generate an array with odd numbers from 1 to 100
307 perl -le '@odd = grep {$_ % 2 == 1} 1..100; print "@odd"'
308 perl -le '@odd = grep { $_ & 1 } 1..100; print "@odd"'
309
310 # Generate an array with even numbers from 1 to 100
311 perl -le '@even = grep {$_ % 2 == 0} 1..100; print "@even"'
312
313 # Find the length of the string
314 perl -le 'print length "one-liners are great"'
315
316 # Find the number of elements in an array
317 perl -le '@array = ("a".."z"); print scalar @array'
318 perl -le '@array = ("a".."z"); print $#array + 1'
319
320
9822c02 @pkrumins Revert TEXT CONVERSION AND SUBSTITUTION title back from the previous …
authored Nov 21, 2011
321 TEXT CONVERSION AND SUBSTITUTION
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
322 --------------------------------
323
324 # ROT13 a string
325 'y/A-Za-z/N-ZA-Mn-za-m/'
326
327 # ROT 13 a file
328 perl -lpe 'y/A-Za-z/N-ZA-Mn-za-m/' file
329
330 # Base64 encode a string
331 perl -MMIME::Base64 -e 'print encode_base64("string")'
332 perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' file
333
334 # Base64 decode a string
335 perl -MMIME::Base64 -le 'print decode_base64("base64string")'
336 perl -MMIME::Base64 -ne 'print decode_base64($_)' file
337
338 # URL-escape a string
339 perl -MURI::Escape -le 'print uri_escape($string)'
340
341 # URL-unescape a string
342 perl -MURI::Escape -le 'print uri_unescape($string)'
343
344 # HTML-encode a string
345 perl -MHTML::Entities -le 'print encode_entities($string)'
346
347 # HTML-decode a string
348 perl -MHTML::Entities -le 'print decode_entities($string)'
349
350 # Convert all text to uppercase
351 perl -nle 'print uc'
352 perl -ple '$_=uc'
353 perl -nle 'print "\U$_"'
354
355 # Convert all text to lowercase
356 perl -nle 'print lc'
357 perl -ple '$_=lc'
358 perl -nle 'print "\L$_"'
359
360 # Uppercase only the first word of each line
361 perl -nle 'print ucfirst lc'
362 perl -nle 'print "\u\L$_"'
363
364 # Invert the letter case
365 perl -ple 'y/A-Za-z/a-zA-Z/'
366
367 # Camel case each line
368 perl -ple 's/(\w+)/\u$1/g'
369 perl -ple 's/(?<!['])(\w+)/\u\1/g'
370
371 # Strip leading whitespace (spaces, tabs) from the beginning of each line
372 perl -ple 's/^[ \t]+//'
373 perl -ple 's/^\s+//'
374
375 # Strip trailing whitespace (space, tabs) from the end of each line
376 perl -ple 's/[ \t]+$//'
377
378 # Strip whitespace from the beginning and end of each line
379 perl -ple 's/^[ \t]+|[ \t]+$//g'
380
381 # Convert UNIX newlines to DOS/Windows newlines
382 perl -pe 's|\n|\r\n|'
383
384 # Convert DOS/Windows newlines to UNIX newlines
385 perl -pe 's|\r\n|\n|'
386
387 # Convert UNIX newlines to Mac newlines
388 perl -pe 's|\n|\r|'
389
390 # Substitute (find and replace) "foo" with "bar" on each line
391 perl -pe 's/foo/bar/'
392
393 # Substitute (find and replace) all "foo"s with "bar" on each line
394 perl -pe 's/foo/bar/g'
395
396 # Substitute (find and replace) "foo" with "bar" on lines that match "baz"
397 perl -pe '/baz/ && s/foo/bar/'
398
1a05619 Added a way to binary patch a file using a sample byte array (and gen…
Emily Strickland authored Nov 20, 2011
399 # Binary patch a file (find and replace a given array of bytes as hex numbers)
400 perl -pi -e 's/\x89\xD8\x48\x8B/\x90\x90\x48\x8B/g' file
401
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
402
403 SELECTIVE PRINTING AND DELETING OF CERTAIN LINES
404 ------------------------------------------------
405
406 # Print the first line of a file (emulate head -1)
407 perl -ne 'print; exit'
408
409 # Print the first 10 lines of a file (emulate head -10)
410 perl -ne 'print if $. <= 10'
411 perl -ne '$. <= 10 && print'
1472e98 @avar Add a shorter way to print the first 10 lines using the flip-flop ope…
avar authored Dec 1, 2011
412 perl -ne 'print if 1..10'
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
413
414 # Print the last line of a file (emulate tail -1)
415 perl -ne '$last = $_; END { print $last }'
416 perl -ne 'print if eof'
417
418 # Print the last 10 lines of a file (emulate tail -10)
419 perl -ne 'push @a, $_; @a = @a[@a-10..$#a]; END { print @a }'
420
421 # Print only lines that match a regular expression
422 perl -ne '/regex/ && print'
423
424 # Print only lines that do not match a regular expression
425 perl -ne '!/regex/ && print'
426
427 # Print the line before a line that matches a regular expression
428 perl -ne '/regex/ && $last && print $last; $last = $_'
429
430 # Print the line after a line that matches a regular expression
431 perl -ne 'if ($p) { print; $p = 0 } $p++ if /regex/'
432
433 # Print lines that match regex AAA and regex BBB in any order
434 perl -ne '/AAA/ && /BBB/ && print'
435
436 # Print lines that don't match match regexes AAA and BBB
437 perl -ne '!/AAA/ && !/BBB/ && print'
438
439 # Print lines that match regex AAA followed by regex BBB followed by CCC
440 perl -ne '/AAA.*BBB.*CCC/ && print'
441
442 # Print lines that are 80 chars or longer
443 perl -ne 'print if length >= 80'
444
445 # Print lines that are less than 80 chars in length
446 perl -ne 'print if length < 80'
447
448 # Print only line 13
449 perl -ne '$. == 13 && print && exit'
450
451 # Print all lines except line 27
452 perl -ne '$. != 27 && print'
453 perl -ne 'print if $. != 27'
454
455 # Print only lines 13, 19 and 67
456 perl -ne 'print if $. == 13 || $. == 19 || $. == 67'
457 perl -ne 'print if int($.) ~~ (13, 19, 67)'
458
459 # Print all lines between two regexes (including lines that match regex)
460 perl -ne 'print if /regex1/../regex2/'
461
462 # Print all lines from line 17 to line 30
463 perl -ne 'print if $. >= 17 && $. <= 30'
464 perl -ne 'print if int($.) ~~ (17..30)'
465 perl -ne 'print if grep { $_ == $. } 17..30'
466
467 # Print the longest line
468 perl -ne '$l = $_ if length($_) > length($l); END { print $l }'
469
470 # Print the shortest line
471 perl -ne '$s = $_ if $. == 1; $s = $_ if length($_) < length($s); END { print $s }'
472
473 # Print all lines that contain a number
474 perl -ne 'print if /\d/'
475
476 # Find all lines that contain only a number
477 perl -ne 'print if /^\d+$/'
478
479 # Print all lines that contain only characters
480 perl -ne 'print if /^[[:alpha:]]+$/
481
482 # Print every second line
483 perl -ne 'print if $. % 2'
484
485 # Print every second line, starting the second line
486 perl -ne 'print if $. % 2 == 0'
487
488 # Print all lines that repeat
489 perl -ne 'print if ++$a{$_} == 2'
490
491 # Print all unique lines
492 perl -ne 'print unless $a{$_}++'
493
d848670 @pkrumins add rent0n's one-liner
authored Dec 22, 2011
494 # Print the first field (word) of every line (emulate cut -f 1 -d ' ')
495 perl -alne 'print $F[0]'
496
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
497
498 HANDY REGULAR EXPRESSIONS
499 -------------------------
500
501 # Match something that looks like an IP address
502 /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
503 /^(\d{1,3}\.){3}\d{1,3}$/
504
505 # Test if a number is in range 0-255
506 /^([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/
507
508 # Match an IP address
509 my $ip_part = qr|([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|;
510 if ($ip =~ /^($ip_part\.){3}$ip_part$/) {
511 say "valid ip";
512 }
513
514 # Check if the string looks like an email address
b672676 @pkrumins better regex for email matching
authored Nov 20, 2011
515 /\S+@\S+\.\S+/
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
516
517 # Check if the string is a decimal number
518 /^\d+$/
519 /^[+-]?\d+$/
520 /^[+-]?\d+\.?\d*$/
521
522 # Check if the string is a hexadecimal number
523 /^0x[0-9a-f]+$/i
524
525 # Check if the string is an octal number
526 /^0[0-7]+$/
527
528 # Check if the string is binary
529 /^[01]+$/
530
531 # Check if a word appears twice in the string
532 /(word).*\1/
533
534 # Increase all numbers by one in the string
535 $str =~ s/(\d+)/$1+1/ge
536
537 # Extract HTTP User-Agent string from the HTTP headers
538 /^User-Agent: (.+)$/
539
540 # Match printable ASCII characters
541 /[ -~]/
542
543 # Match unprintable ASCII characters
544 /[^ -~]/
545
546 # Match text between two HTML tags
547 m|<strong>([^<]*)</strong>|
548 m|<strong>(.*?)</strong>|
549
550 # Replace all <b> tags with <strong>
551 $html =~ s|<(/)?b>|<$1strong>|g
552
553 # Extract all matches from a regular expression
554 my @matches = $text =~ /regex/g;
555
556
a820a8a @pkrumins added perl tricks section. added print the version of a perl module
authored Nov 14, 2011
557 PERL TRICKS
558 -----------
559
560 # Print the version of a Perl module
561 perl -MModule -le 'print $Module::VERSION'
562 perl -MLWP::UserAgent -le 'print $LWP::UserAgent::VERSION'
563
564
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
565 PERL ONE-LINERS EXPLAINED E-BOOK
566 --------------------------------
567
e752f32 @pkrumins Perl One-Liners Explained e-book is out!
authored Jan 28, 2012
568 I have written an ebook based on the one-liners in this file. If you wish to
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
569 support my work and learn more about these one-liners, you can get a copy
e752f32 @pkrumins Perl One-Liners Explained e-book is out!
authored Jan 28, 2012
570 of my ebook at:
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
571
572 http://www.catonmat.net/blog/perl-book/
573
574 The ebook is based on the 7-part article series that I wrote on my blog.
e752f32 @pkrumins Perl One-Liners Explained e-book is out!
authored Jan 28, 2012
575 In the ebook I reviewed all the one-liners, improved explanations, added
576 new ones, and added two new chapters - introduction to Perl one-liners
577 and summary of commonly used special variables.
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
578
579 You can read the original article series here:
580
581 http://www.catonmat.net/blog/perl-one-liners-explained-part-one/
582 http://www.catonmat.net/blog/perl-one-liners-explained-part-two/
583 http://www.catonmat.net/blog/perl-one-liners-explained-part-three/
584 http://www.catonmat.net/blog/perl-one-liners-explained-part-four/
585 http://www.catonmat.net/blog/perl-one-liners-explained-part-five/
586 http://www.catonmat.net/blog/perl-one-liners-explained-part-six/
587 http://www.catonmat.net/blog/perl-one-liners-explained-part-seven/
588
589
590 CREDITS
591 -------
592
593 Andy Lester http://www.petdance.com
e4d3ac9 @pkrumins shlomi wants .org
authored Nov 15, 2011
594 Shlomi Fish http://www.shlomifish.org
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
595 Madars Virza http://www.madars.org
d91a66e @pkrumins v1.03 now. add caffecaldo to credits
authored Nov 16, 2011
596 caffecaldo https://github.com/caffecaldo
8998dbd @pkrumins 1.04 now. added Kirk Kimmel to credits
authored Nov 17, 2011
597 Kirk Kimmel https://github.com/kimmel
95c3553 @pkrumins add avar to credits. v1.06 now.
authored Dec 1, 2011
598 avar https://github.com/avar
d848670 @pkrumins add rent0n's one-liner
authored Dec 22, 2011
599 rent0n
912af07 @pkrumins added perl1line.txt
authored Nov 14, 2011
600
601
602 FOUND A BUG? HAVE ANOTHER ONE-LINER?
603 ------------------------------------
604
605 Email bugs and new one-liners to me at peter@catonmat.net!
606
607
608 HAVE FUN
609 --------
610
611 I hope you found these one-liners useful. Have fun!
612
613 #---end of file---
Something went wrong with that request. Please try again.