Synopses & Reviews
Part of the fun of programming in Perl lies in tackling tedious tasks with short, efficient, and reusable code. Often, the perfect tool is the one-liner, a small but powerful program that fits in one line of code and does one thing really well.
In Perl One-Liners, author and impatient hacker Peteris Krumins takes you through more than 100 compelling one-liners that do all sorts of handy things, such as manipulate line spacing, tally column values in a table, and get a list of users on a system. This cookbook of useful, customizable, and fun scripts will even help hone your Perl coding skills, as Krumins dissects the code to give you a deeper understanding of the language.
You'll find one-liners that:
- Encode, decode, and convert strings
- Generate random passwords
- Calculate sums, factorials, and the mathematical constants π and e
- Add or remove spaces
- Number lines in a file
- Print lines that match a specific pattern
- Check to see if a number is prime with a regular expression
- Convert IP address to decimal form
- Replace one string with another
And many more! Save time and sharpen you coding skills as you learn to conquer those pesky tasks in a few precisely placed keystrokes with Perl One-Liners
Perl one-liners are small and awesome Perl programs that fit in one line of code and do one thing really well. Perl One-Liners showcases 130 short and compelling lines of code that do all sorts of handy, geeky things like numbering lines in a file; generating random passwords; encoding, decoding, and converting strings; calculating factorials; even checking to see if a number is prime with a regular expression. Author Peteris Krumins' dissections of each bit of code will help you gain a deeper understanding of the Perl language, and these Perl one-liners are sure to save you time and sharpen your sleight of hand.
About the Author
Peteris Krumins is a programmer, systems administrator, start-up manager, and all-around hacker. He is the cofounder of Browserling and Testling, and he runs the popular programming blog catonmat.net.
Table of Contents
About the Author; About the Technical Reviewer; Acknowledgments; Chapter 1: Introduction to Perl One-Liners; Chapter 2: Spacing; 2.1 2.1 Double-space a file; 2.2 2.2 Double-space a file, excluding the blank lines; 2.3 2.3 Triple-space a file; 2.4 2.4 N-space a file; 2.5 2.5 Add a blank line before every line; 2.6 2.6 Remove all blank lines; 2.7 2.7 Remove all consecutive blank lines, leaving only one; 2.8 2.8 Compress/expand all blank lines into N consecutive lines; 2.9 2.9 Double-space between all words; 2.10 2.10 Remove all spacing between words; 2.11 2.11 Change all spacing between words to one space; 2.12 2.12 Insert a space between all characters; Chapter 3: Numbering; 3.1 3.1 Number all lines in a file; 3.2 3.2 Number only non-empty lines in a file; 3.3 3.3 Number and print only non-empty lines in a file (drop empty lines); 3.4 3.4 Number all lines but print line numbers only for non-empty lines; 3.5 3.5 Number only lines that match a pattern; print others unmodified; 3.6 3.6 Number and print only lines that match a pattern; 3.7 3.7 Number all lines but print line numbers only for lines that match a pattern; 3.8 3.8 Number all lines in a file using a custom format; 3.9 3.9 Print the total number of lines in a file (emulate wc -l); 3.10 3.10 Print the number of non-empty lines in a file; 3.11 3.11 Print the number of empty lines in a file; 3.12 3.12 Print the number of lines in a file that match a pattern (emulate grep -c); 3.13 3.13 Number words across all lines; 3.14 3.14 Number words on each individual line; 3.15 3.15 Replace all words with their numeric positions; Chapter 4: Calculations; 4.1 4.1 Check if a number is a prime; 4.2 4.2 Print the sum of all fields on each line; 4.3 4.3 Print the sum of all fields on all lines; 4.4 4.4 Shuffle all fields on each line; 4.5 4.5 Find the numerically smallest element (minimum element) on each line; 4.6 4.6 Find the numerically smallest element (minimum element) over all lines; 4.7 4.7 Find the numerically largest element (maximum element) on each line; 4.8 4.8 Find the numerically largest element (maximum element) over all lines; 4.9 4.9 Replace each field with its absolute value; 4.10 4.10 Print the total number of fields on each line; 4.11 4.11 Print the total number of fields on each line, followed by the line; 4.12 4.12 Print the total number of fields on all lines; 4.13 4.13 Print the total number of fields that match a pattern; 4.14 4.14 Print the total number of lines that match a pattern; 4.15 4.15 Print the number π; 4.16 4.16 Print the number e; 4.17 4.17 Print UNIX time (seconds since January 1, 1970, 00:00:00 UTC); 4.18 4.18 Print Greenwich Mean Time and local computer time; 4.19 4.19 Print yesterday's date; 4.20 4.20 Print the date 14 months, 9 days, and 7 seconds ago; 4.21 4.21 Calculate the factorial; 4.22 4.22 Calculate the greatest common divisor; 4.23 4.23 Calculate the least common multiple; 4.24 4.24 Generate 10 random numbers between 5 and 15 (excluding 15); 4.25 4.25 Generate all permutations of a list; 4.26 4.26 Generate the powerset; 4.27 4.27 Convert an IP address to an unsigned integer; 4.28 4.28 Convert an unsigned integer to an IP address; Chapter 5: Working With Arrays and Strings; 5.1 5.1 Generate and print the alphabet; 5.2 5.2 Generate and print all the strings from "a" to "zz"; 5.3 5.3 Create a hex lookup table; 5.4 5.4 Generate a random eight-character password; 5.5 5.5 Create a string of specific length; 5.6 5.6 Create an array from a string; 5.7 5.7 Create a string from the command-line arguments; 5.8 5.8 Find the numeric values for characters in a string; 5.9 5.9 Convert a list of numeric ASCII values into a string; 5.10 5.10 Generate an array with odd numbers from 1 to 100; 5.11 5.11 Generate an array with even numbers from 1 to 100; 5.12 5.12 Find the length of a string; 5.13 5.13 Find the number of elements in an array; Chapter 6: Text Conversion and Substitution; 6.1 6.1 ROT13 a string; 6.2 6.2 Base64-encode a string; 6.3 6.3 Base64-decode a string; 6.4 6.4 URL-escape a string; 6.5 6.5 URL-unescape a string; 6.6 6.6 HTML-encode a string; 6.7 6.7 HTML-decode a string; 6.8 6.8 Convert all text to uppercase; 6.9 6.9 Convert all text to lowercase; 6.10 6.10 Uppercase only the first letter of each line; 6.11 6.11 Invert the letter case; 6.12 6.12 Title-case each line; 6.13 6.13 Strip leading whitespace (spaces, tabs) from the beginning of each line; 6.14 6.14 Strip trailing whitespace (spaces, tabs) from the end of each line; 6.15 6.15 Strip whitespace (spaces, tabs) from the beginning and end of each line; 6.16 6.16 Convert UNIX newlines to DOS/Windows newlines; 6.17 6.17 Convert DOS/Windows newlines to UNIX newlines; 6.18 6.18 Convert UNIX newlines to Mac newlines; 6.19 6.19 Substitute (find and replace) "foo" with "bar" on each line; 6.20 6.20 Substitute (find and replace) "foo" with "bar" on lines that match "baz"; 6.21 6.21 Print paragraphs in reverse order; 6.22 6.22 Print all lines in reverse order; 6.23 6.23 Print columns in reverse order; Chapter 7: Selectively Printing and Deleting Lines; 7.1 7.1 Print the first line of a file (emulate head -1); 7.2 7.2 Print the first 10 lines of a file (emulate head -10); 7.3 7.3 Print the last line of a file (emulate tail -1); 7.4 7.4 Print the last 10 lines of a file (emulate tail -10); 7.5 7.5 Print only lines that match a regular expression; 7.6 7.6 Print only lines that do not match a regular expression; 7.7 7.7 Print every line preceding a line that matches a regular expression; 7.8 7.8 Print every line following a line that matches a regular expression; 7.9 7.9 Print lines that match regular expressions AAA and BBB in any order; 7.10 7.10 Print lines that don't match regular expressions AAA and BBB; 7.11 7.11 Print lines that match regular expression AAA followed by BBB followed by CCC; 7.12 7.12 Print lines that are at least 80 characters long; 7.13 7.13 Print lines that are fewer than 80 characters long; 7.14 7.14 Print only line 13; 7.15 7.15 Print all lines except line 27; 7.16 7.16 Print only lines 13, 19, and 67; 7.17 7.17 Print all lines from 17 to 30; 7.18 7.18 Print all lines between two regular expressions (including the lines that match); 7.19 7.19 Print the longest line; 7.20 7.20 Print the shortest line; 7.21 7.21 Print all lines containing digits; 7.22 7.22 Print all lines containing only digits; 7.23 7.23 Print all lines containing only alphabetic characters; 7.24 7.24 Print every second line; 7.25 7.25 Print every second line, beginning with the second line; 7.26 7.26 Print all repeated lines only once; 7.27 7.27 Print all unique lines; Chapter 8: Useful Regular Expressions; 8.1 8.1 Match something that looks like an IP address; 8.2 8.2 Test whether a number is in the range 0 to 255; 8.3 8.3 Match an IP address; 8.4 8.4 Check whether a string looks like an email address; 8.5 8.5 Check whether a string is a number; 8.6 8.6 Check whether a word appears in a string twice; 8.7 8.7 Increase all integers in a string by one; 8.8 8.8 Extract the HTTP User-Agent string from HTTP headers; 8.9 8.9 Match printable ASCII characters; 8.10 8.10 Extract text between two HTML tags; 8.11 8.11 Replace all tags with ; 8.12 8.12 Extract all matches from a regular expression; Perl's Special Variables; A.1 Variable $_; A.2 Variable $.; A.3 Variable $/; A.4 Variable $\; A.5 Variables $1, $2, $3, and so on; A.6 Variable $,; A.7 Variable $"; A.8 Variable @F; A.9 Variable @ARGV; A.10 Variable %ENV; Using Perl One-Liners On Windows; B.1 Perl on Windows; B.2 Bash on Windows; B.3 Perl One-Liners in Windows Bash; B.4 Perl One-Liners in the Windows Command Prompt; B.5 Perl One-Liners in PowerShell; Perl1Line.Txt; C.1 Spacing; C.2 Numbering; C.3 Calculations; C.4 Working with Arrays and Strings; C.5 Text Conversion and Substitution; C.6 Selectively Printing and Deleting Lines; C.7 Useful Regular Expressions;