# Double space a file perl -pe '$\="\n"' perl -pe 'BEGIN { $\="\n" }' perl -pe '$_ .= "\n"' perl -pe 's/$/\n/' # Double space a file, except the blank lines perl -pe '$_ .= "\n" unless /^$/' perl -pe '$_ .= "\n" if /\S/' # Triple space a file perl -pe '$\="\n\n"' perl -pe '$_.="\n\n"' # N-space a file perl -pe '$_.="\n"x7' # Add a blank line before every line perl -pe 's//\n/' # Remove all blank lines perl -ne 'print unless /^$/' perl -lne 'print if length' perl -ne 'print if /\S/' # Remove all consecutive blank lines, leaving just one perl -00 -pe '' perl -00pe0 # Compress/expand all blank lines into N consecutive ones perl -00 -pe '$_.="\n"x4' # Fold a file so that every set of 10 lines becomes one tab-separated line perl -lpe '$\ = $. % 10 ? "\t" : "\n"' # Number all lines in a file perl -pe '$_ = "$. $_"' # Number only non-empty lines in a file perl -pe '$_ = ++$a." $_" if /./' # 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 only lines that match a pattern, print others unmodified perl -pe '$_ = ++$a." $_" if /regex/' # Number and print only lines that match a pattern perl -ne 'print ++$a." $_" if /regex/' # 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 total number of lines in a file (emulate wc -l) perl -lne 'END { print $. }' perl -le 'print $n=()=<>' perl -le 'print scalar(()=<>)' perl -le 'print scalar(@foo=<>)' perl -ne '}{print $.' perl -nE '}{say $.' # Print the number of non-empty lines in a file perl -le 'print scalar(grep{/./}<>)' perl -le 'print ~~grep{/./}<>' perl -le 'print~~grep/./,<>' perl -E 'say~~grep/./,<>' # Print the number of empty lines in a file perl -lne '$a++ if /^$/; END {print $a+0}' perl -le 'print scalar(grep{/^$/}<>)' perl -le 'print ~~grep{/^$/}<>' perl -E 'say~~grep{/^$/}<>' # 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}' # Convert an IP address to unsigned integer perl -le '$i=3; $u += ($_<<8*$i--) for "127.0.0.1" =~ /(\d+)/g; print $u' perl -le '$ip="127.0.0.1"; $ip =~ s/(\d+)\.?/sprintf("%02x", $1)/ge; print hex($ip)' perl -le 'print unpack("N", 127.0.0.1)' perl -MSocket -le 'print unpack("N", inet_aton("127.0.0.1"))' # Convert an unsigned integer to an IP address perl -MSocket -le 'print inet_ntoa(pack("N", 2130706433))' perl -le '$ip = 2130706433; print join ".", map { (($ip>>8*($_))&0xFF) } reverse 0..3' perl -le '$ip = 2130706433; $, = "."; print map { (($ip>>8*($_))&0xFF) } reverse 0..3' # Generate and print the alphabet perl -le 'print a..z' perl -le 'print ("a".."z")' perl -le '$, = ","; print ("a".."z")' perl -le 'print join ",", ("a".."z")' # Generate and print all the strings from "a" to "zz" perl -le 'print ("a".."zz")' perl -le 'print "aa".."zz"' # Create a hex lookup table @hex = (0..9, "a".."f") # Convert a decimal number to hex using @hex lookup table perl -le '$num = 255; @hex = (0..9, "a".."f"); while ($num) { $s = $hex[($num%16)&15].$s; $num = int $num/16 } print $s' perl -le '$hex = sprintf("%x", 255); print $hex' perl -le '$num = "ff"; print hex $num' # Generate a random 8 character password perl -le 'print map { ("a".."z")[rand 26] } 1..8' perl -le 'print map { ("a".."z", 0..9)[rand 36] } 1..8' # Create a string of specific length perl -le 'print "a"x50' # Create a repeated list of elements perl -le '@list = (1,2)x20; print "@list"' # Create an array from a string @months = split ' ', "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec" @months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/ # Create a string from an array @stuff = ("hello", 0..9, "world"); $string = join '-', @stuff # Find the numeric values for characters in the string perl -le 'print join ", ", map { ord } split //, "hello world"' # Convert a list of numeric ASCII values into a string perl -le '@ascii = (99, 111, 100, 105, 110, 103); print pack("C*", @ascii)' perl -le '@ascii = (99, 111, 100, 105, 110, 103); print map { chr } @ascii' # Generate an array with odd numbers from 1 to 100 perl -le '@odd = grep {$_ % 2 == 1} 1..100; print "@odd"' perl -le '@odd = grep { $_ & 1 } 1..100; print "@odd"' # Generate an array with even numbers from 1 to 100 perl -le '@even = grep {$_ % 2 == 0} 1..100; print "@even"' # Find the length of the string perl -le 'print length "one-liners are great"' # Find the number of elements in an array perl -le '@array = ("a".."z"); print scalar @array' perl -le '@array = ("a".."z"); print $#array + 1' # 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 # Base64 encode a string perl -MMIME::Base64 -e 'print encode_base64("string")' perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)' file # Base64 decode a string perl -MMIME::Base64 -le 'print decode_base64("base64string")' perl -MMIME::Base64 -ne 'print decode_base64($_)' 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)' # Convert all text to uppercase perl -nle 'print uc' perl -ple '$_=uc' perl -nle 'print "\U$_"' # Convert all text to lowercase perl -nle 'print lc' perl -ple '$_=lc' perl -nle 'print "\L$_"' # Uppercase only the first word of each line perl -nle 'print ucfirst lc' perl -nle 'print "\u\L$_"' # Invert the letter case perl -ple 'y/A-Za-z/a-zA-Z/' # Camel case each line perl -ple 's/(\w+)/\u$1/g' perl -ple 's/(?= 80' # Print lines that are less than 80 chars in length perl -ne 'print if length < 80' # Print only line 13 perl -ne '$. == 13 && print && exit' # Print all lines except line 27 perl -ne '$. != 27 && print' perl -ne 'print if $. != 27' # Print only lines 13, 19 and 67 perl -ne 'print if $. == 13 || $. == 19 || $. == 67' perl -ne 'print if int($.) ~~ (13, 19, 67)' # 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 longest line perl -ne '$l = $_ if length($_) > length($l); END { print $l }' # Print the shortest line perl -ne '$s = $_ if $. == 1; $s = $_ if length($_) < length($s); END { print $s }' # Print all lines that contain a number perl -ne 'print if /\d/' # Find all lines that contain only a number perl -ne 'print if /^\d+$/' # Print all lines that contain only characters perl -ne 'print if /^[[:alpha:]]+$/ # Print every second line perl -ne 'print if $. % 2' # Print every second line, starting the second line perl -ne 'print if $. % 2 == 0' # Print all lines that repeat perl -ne 'print if ++$a{$_} == 2' # Print all unique lines perl -ne 'print unless $a{$_}++' # Print the first field (word) of every line (emulate cut -f 1 -d ' ') perl -alne 'print $F[0]' # Match something that looks like an IP address /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ /^(\d{1,3}\.){3}\d{1,3}$/ # Test if a number is in range 0-255 /^([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/ # 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"; } # Check if the string looks like an email address /\S+@\S+\.\S+/ # Check if the string is a decimal number /^\d+$/ /^[+-]?\d+$/ /^[+-]?\d+\.?\d*$/ # Check if the string is a hexadecimal number /^0x[0-9a-f]+$/i # Check if the string is an octal number /^0[0-7]+$/ # Check if the string is binary /^[01]+$/ # Check if a word appears twice in the string /(word).*\1/ # Increase all numbers by one in the string $str =~ s/(\d+)/$1+1/ge # Extract HTTP User-Agent string from the HTTP headers /^User-Agent: (.+)$/ # Match printable ASCII characters /[ -~]/ # Match unprintable ASCII characters /[^ -~]/ # Match text between two HTML tags m|([^<]*)| m|(.*?)| # Replace all tags with $html =~ s|<(/)?b>|<$1strong>|g # Extract all matches from a regular expression my @matches = $text =~ /regex/g; # Print the version of a Perl module perl -MModule -le 'print $Module::VERSION' perl -MLWP::UserAgent -le 'print $LWP::UserAgent::VERSION'