Perl Onliners
Perl Onliners
11
-------------------------------- ----------- ------------
http://www.catonmat.net/download/perl1line.txt
Chinese: https://github.com/vinian/perl1line.txt
https://github.com/pkrumins/perl1line.txt
You can send me pull requests over GitHub! I accept bug fixes,
new one-liners, translations and everything else related.
http://www.catonmat.net/blog/perl-book/
http://nostarch.com/perloneliners
These one-liners work both on UNIX systems and Windows. Most likely your
UNIX system already has Perl. For Windows get the Strawberry Perl at:
http://www.strawberryperl.com/
Table of contents:
1. File Spacing
2. Line Numbering
3. Calculations
4. String Creation and Array Creation
5. Text Conversion and Substitution
6. Selective Printing and Deleting of Certain Lines
7. Handy Regular Expressions
8. Perl tricks
FILE SPACING
------------
# N-space a file
perl -pe '$_.="\n"x7'
# Fold a file so that every set of 10 lines becomes one tab-separated line
perl -lpe '$\ = $. % 10 ? "\t" : "\n"'
LINE NUMBERING
--------------
# Number and print only non-empty lines in a file (drop empty lines)
perl -ne 'print ++$a." $_" if /./'
# Number all lines but print line numbers only non-empty lines
perl -pe '$_ = "$. $_" if /./'
# Number all lines, but print line numbers only for lines that match a pattern
perl -pe '$_ = "$. $_" if /regex/'
# Number all lines in a file using a custom format (emulate cat -n)
perl -ne 'printf "%-5d %s", $., $_'
# Print the number of lines in a file that match a pattern (emulate grep -c)
perl -lne '$a++ if /regex/; END {print $a+0}'
perl -nE '$a++ if /regex/; END {say $a+0}'
CALCULATIONS
------------
# Calculate factorial of 5
perl -MMath::BigInt -le 'print Math::BigInt->new(5)->bfac()'
perl -le '$f = 1; $f *= $_ for 1..5; print $f'
# ROT13 a string
'y/A-Za-z/N-ZA-Mn-za-m/'
# ROT 13 a file
perl -lpe 'y/A-Za-z/N-ZA-Mn-za-m/' file
# URL-escape a string
perl -MURI::Escape -le 'print uri_escape($string)'
# URL-unescape a string
perl -MURI::Escape -le 'print uri_unescape($string)'
# HTML-encode a string
perl -MHTML::Entities -le 'print encode_entities($string)'
# HTML-decode a string
perl -MHTML::Entities -le 'print decode_entities($string)'
# Strip leading whitespace (spaces, tabs) from the beginning of each line
perl -ple 's/^[ \t]+//'
perl -ple 's/^\s+//'
# Strip trailing whitespace (space, tabs) from the end of each line
perl -ple 's/[ \t]+$//'
# Substitute (find and replace) all "foo"s with "bar" on each line
perl -pe 's/foo/bar/g'
# Substitute (find and replace) "foo" with "bar" on lines that match "baz"
perl -pe '/baz/ && s/foo/bar/'
# Binary patch a file (find and replace a given array of bytes as hex numbers)
perl -pi -e 's/\x89\xD8\x48\x8B/\x90\x90\x48\x8B/g' file
SELECTIVE PRINTING AND DELETING OF CERTAIN LINES
------------------------------------------------
# Print lines that match regex AAA and regex BBB in any order
perl -ne '/AAA/ && /BBB/ && print'
# Print lines that don't match match regexes AAA and BBB
perl -ne '!/AAA/ && !/BBB/ && print'
# Print lines that match regex AAA followed by regex BBB followed by CCC
perl -ne '/AAA.*BBB.*CCC/ && print'
# Print all lines between two regexes (including lines that match regex)
perl -ne 'print if /regex1/../regex2/'
# Print all lines from line 17 to line 30
perl -ne 'print if $. >= 17 && $. <= 30'
perl -ne 'print if int($.) ~~ (17..30)'
perl -ne 'print if grep { $_ == $. } 17..30'
# Print the first field (word) of every line (emulate cut -f 1 -d ' ')
perl -alne 'print $F[0]'
# Match an IP address
my $ip_part = qr|([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|;
if ($ip =~ /^($ip_part\.){3}$ip_part$/) {
say "valid ip";
}
PERL TRICKS
-----------
I have written an ebook based on the one-liners in this file. If you wish to
support my work and learn more about these one-liners, you can get a copy
of my ebook at:
http://www.catonmat.net/blog/perl-book/
The ebook is based on the 7-part article series that I wrote on my blog.
In the ebook I reviewed all the one-liners, improved explanations, added
new ones, and added two new chapters - introduction to Perl one-liners
and summary of commonly used special variables.
CREDITS
-------
HAVE FUN
--------
#---end of file---