Book description
Perl One-Liners showcases 130 short and compelling lines of code that do all sorts of handy, geeky things.
Table of contents
- About the Author
- About the Technical Reviewer
- Acknowledgments
- 1. Introduction to Perl One-Liners
-
2. Spacing
- 2.1 Double-space a file
- 2.2 Double-space a file, excluding the blank lines
- 2.3 Triple-space a file
- 2.4 N-space a file
- 2.5 Add a blank line before every line
- 2.6 Remove all blank lines
- 2.7 Remove all consecutive blank lines, leaving only one
- 2.8 Compress/expand all blank lines into N consecutive lines
- 2.9 Double-space between all words
- 2.10 Remove all spacing between words
- 2.11 Change all spacing between words to one space
- 2.12 Insert a space between all characters
-
3. Numbering
- 3.1 Number all lines in a file
- 3.2 Number only non-empty lines in a file
- 3.3 Number and print only non-empty lines in a file (drop empty lines)
- 3.4 Number all lines but print line numbers only for non-empty lines
- 3.5 Number only lines that match a pattern; print others unmodified
- 3.6 Number and print only lines that match a pattern
- 3.7 Number all lines but print line numbers only for lines that match a pattern
- 3.8 Number all lines in a file using a custom format
- 3.9 Print the total number of lines in a file (emulate wc -l)
- 3.10 Print the number of non-empty lines in a file
- 3.11 Print the number of empty lines in a file
- 3.12 Print the number of lines in a file that match a pattern (emulate grep -c)
- 3.13 Number words across all lines
- 3.14 Number words on each individual line
- 3.15 Replace all words with their numeric positions
-
4. Calculations
- 4.1 Check if a number is a prime
- 4.2 Print the sum of all fields on each line
- 4.3 Print the sum of all fields on all lines
- 4.4 Shuffle all fields on each line
- 4.5 Find the numerically smallest element (minimum element) on each line
- 4.6 Find the numerically smallest element (minimum element) over all lines
- 4.7 Find the numerically largest element (maximum element) on each line
- 4.8 Find the numerically largest element (maximum element) over all lines
- 4.9 Replace each field with its absolute value
- 4.10 Print the total number of fields on each line
- 4.11 Print the total number of fields on each line, followed by the line
- 4.12 Print the total number of fields on all lines
- 4.13 Print the total number of fields that match a pattern
- 4.14 Print the total number of lines that match a pattern
- 4.15 Print the number π
- 4.16 Print the number e
- 4.17 Print UNIX time (seconds since January 1, 1970, 00:00:00 UTC)
- 4.18 Print Greenwich Mean Time and local computer time
- 4.19 Print yesterday’s date
- 4.20 Print the date 14 months, 9 days, and 7 seconds ago
- 4.21 Calculate the factorial
- 4.22 Calculate the greatest common divisor
- 4.23 Calculate the least common multiple
- 4.24 Generate 10 random numbers between 5 and 15 (excluding 15)
- 4.25 Generate all permutations of a list
- 4.26 Generate the powerset
- 4.27 Convert an IP address to an unsigned integer
- 4.28 Convert an unsigned integer to an IP address
-
5. Working With Arrays and Strings
- 5.1 Generate and print the alphabet
- 5.2 Generate and print all the strings from “a” to “zz”
- 5.3 Create a hex lookup table
- 5.4 Generate a random eight-character password
- 5.5 Create a string of specific length
- 5.6 Create an array from a string
- 5.7 Create a string from the command-line arguments
- 5.8 Find the numeric values for characters in a string
- 5.9 Convert a list of numeric ASCII values into a string
- 5.10 Generate an array with odd numbers from 1 to 100
- 5.11 Generate an array with even numbers from 1 to 100
- 5.12 Find the length of a string
- 5.13 Find the number of elements in an array
-
6. Text Conversion and Substitution
- 6.1 ROT13 a string
- 6.2 Base64-encode a string
- 6.3 Base64-decode a string
- 6.4 URL-escape a string
- 6.5 URL-unescape a string
- 6.6 HTML-encode a string
- 6.7 HTML-decode a string
- 6.8 Convert all text to uppercase
- 6.9 Convert all text to lowercase
- 6.10 Uppercase only the first letter of each line
- 6.11 Invert the letter case
- 6.12 Title-case each line
- 6.13 Strip leading whitespace (spaces, tabs) from the beginning of each line
- 6.14 Strip trailing whitespace (spaces, tabs) from the end of each line
- 6.15 Strip whitespace (spaces, tabs) from the beginning and end of each line
- 6.16 Convert UNIX newlines to DOS/Windows newlines
- 6.17 Convert DOS/Windows newlines to UNIX newlines
- 6.18 Convert UNIX newlines to Mac newlines
- 6.19 Substitute (find and replace) “foo” with “bar” on each line
- 6.20 Substitute (find and replace) “foo” with “bar” on lines that match “baz”
- 6.21 Print paragraphs in reverse order
- 6.22 Print all lines in reverse order
- 6.23 Print columns in reverse order
-
7. Selectively Printing and Deleting Lines
- 7.1 Print the first line of a file (emulate head -1)
- 7.2 Print the first 10 lines of a file (emulate head -10)
- 7.3 Print the last line of a file (emulate tail -1)
- 7.4 Print the last 10 lines of a file (emulate tail -10)
- 7.5 Print only lines that match a regular expression
- 7.6 Print only lines that do not match a regular expression
- 7.7 Print every line preceding a line that matches a regular expression
- 7.8 Print every line following a line that matches a regular expression
- 7.9 Print lines that match regular expressions AAA and BBB in any order
- 7.10 Print lines that don’t match regular expressions AAA and BBB
- 7.11 Print lines that match regular expression AAA followed by BBB followed by CCC
- 7.12 Print lines that are at least 80 characters long
- 7.13 Print lines that are fewer than 80 characters long
- 7.14 Print only line 13
- 7.15 Print all lines except line 27
- 7.16 Print only lines 13, 19, and 67
- 7.17 Print all lines from 17 to 30
- 7.18 Print all lines between two regular expressions (including the lines that match)
- 7.19 Print the longest line
- 7.20 Print the shortest line
- 7.21 Print all lines containing digits
- 7.22 Print all lines containing only digits
- 7.23 Print all lines containing only alphabetic characters
- 7.24 Print every second line
- 7.25 Print every second line, beginning with the second line
- 7.26 Print all repeated lines only once
- 7.27 Print all unique lines
-
8. Useful Regular Expressions
- 8.1 Match something that looks like an IP address
- 8.2 Test whether a number is in the range 0 to 255
- 8.3 Match an IP address
- 8.4 Check whether a string looks like an email address
- 8.5 Check whether a string is a number
- 8.6 Check whether a word appears in a string twice
- 8.7 Increase all integers in a string by one
- 8.8 Extract the HTTP User-Agent string from HTTP headers
- 8.9 Match printable ASCII characters
- 8.10 Extract text between two HTML tags
- 8.11 Replace all <b> tags with <strong>
- 8.12 Extract all matches from a regular expression
- A. Perl’s Special Variables
- B. Using Perl One-Liners On Windows
-
C. Perl1Line.Txt
-
C.1 Spacing
-
- Double-space a file
- Double-space a file, excluding the blank lines
- Triple-space a file
- N-space a file
- Add a blank line before every line
- Remove all blank lines
- Remove all consecutive blank lines, leaving only one
- Compress/expand all blank lines into N consecutive lines
- Double-space between all words
- Remove all spacing between words
- Change all spacing between words to one space
- Insert a space between all characters
-
-
C.2 Numbering
-
- Number all lines in a file
- Number only non-empty lines in a file
- Number and print only non-empty lines in a file (drop empty lines)
- Number all lines but print line numbers only for non-empty lines
- Number only lines that match a pattern; print others unmodified
- Number and print only lines that match a pattern
- Number all lines but print line numbers only for lines that match a pattern
- Number all lines in a file using a custom format
- Print the total number of lines in a file (emulate wc -l)
- Print the number of non-empty lines in a file
- Print the number of empty lines in a file
- Print the number of lines in a file that match a pattern (emulate grep -c)
- Number words across all lines
- Number words on each individual line
- Replace all words with their numeric positions
-
-
C.3 Calculations
-
- Check if a number is a prime
- Print the sum of all fields on each line
- Print the sum of all fields on all lines
- Shuffle all fields on each line
- Find the numerically smallest element (minimum element) on each line
- Find the numerically smallest element (minimum element) over all lines
- Find the numerically largest element (maximum element) on each line
- Find the numerically largest element (maximum element) over all lines
- Replace each field with its absolute value
- Print the total number of fields on each line
- Print the total number of fields on each line, followed by the line
- Print the total number of fields on all lines
- Print the total number of fields that match a pattern
- Print the total number of lines that match a pattern
- Print the number π
- Print the number e
- Print UNIX time (seconds since January 1, 1970, 00:00:00 UTC)
- Print Greenwich Mean Time and local computer time
- Print yesterday’s date
- Print the date 14 months, 9 days, and 7 seconds ago
- Calculate the factorial
- Calculate the greatest common divisor
- Calculate the least common multiple
- Generate 10 random numbers between 5 and 15 (excluding 15)
- Generate all permutations of a list
- Generate the powerset
- Convert an IP address to an unsigned integer
- Convert an unsigned integer to an IP address
-
-
C.4 Working with Arrays and Strings
-
- Generate and print the alphabet
- Generate and print all the strings from “a” to “zz”
- Create a hex lookup table
- Generate a random eight-character password
- Create a string of specific length
- Create an array from a string
- Create a string from the command-line arguments
- Find the numeric values for characters in a string
- Convert a list of numeric ASCII values into a string
- Generate an array with odd numbers from 1 to 100
- Generate an array with even numbers from 1 to 100
- Find the length of a string
- Find the number of elements in an array
-
-
C.5 Text Conversion and Substitution
-
- ROT13 a string
- Base64-encode a string
- Base64-decode a string
- URL-escape a string
- URL-unescape a string
- HTML-encode a string
- HTML-decode a string
- Convert all text to uppercase
- Convert all text to lowercase
- Uppercase only the first letter of each line
- Invert the letter case
- Title-case each line
- Strip leading whitespace (spaces, tabs) from the beginning of each line
- Strip trailing whitespace (spaces, tabs) from the end of each line
- Strip whitespace (spaces, tabs) from the beginning and end of each line
- Convert UNIX newlines to DOS/Windows newlines
- Convert DOS/Windows newlines to UNIX newlines
- Convert UNIX newlines to Mac newlines
- Substitute (find and replace) “foo” with “bar” on each line
- Substitute (find and replace) “foo” with “bar” on lines that match “baz”
- Print paragraphs in reverse order
- Print all lines in reverse order
- Print columns in reverse order
-
-
C.6 Selectively Printing and Deleting Lines
-
- Print the first line of a file (emulate head -1)
- Print the first 10 lines of a file (emulate head -10)
- Print the last line of a file (emulate tail -1)
- Print the last 10 lines of a file (emulate tail -10)
- Print only lines that match a regular expression
- Print only lines that do not match a regular expression
- Print every line preceding a line that matches a regular expression
- Print every line following a line that matches a regular expression
- Print lines that match regular expressions AAA and BBB in any order
- Print lines that don’t match regular expressions AAA and BBB
- Print lines that match regular expression AAA followed by BBB followed by CCC
- Print lines that are at least 80 characters long
- Print lines that are fewer than 80 characters long
- Print only line 13
- Print all lines except line 27
- Print only lines 13, 19, and 67
- Print all lines from 17 to 30
- Print all lines between two regular expressions (including the lines that match)
- Print the longest line
- Print the shortest line
- Print all lines containing digits
- Print all lines containing only digits
- Print all lines containing only alphabetic characters
- Print every second line
- Print every second line, beginning with the second line
- Print all repeated lines only once
- Print all unique lines
-
-
C.7 Useful Regular Expressions
-
- Match something that looks like an IP address
- Test whether a number is in the range 0 to 255
- Match an IP address
- Check whether a string looks like an email address
- Check whether a string is a number
- Check whether a word appears in a string twice
- Increase all integers in a string by one
- Extract the HTTP User-Agent string from HTTP headers
- Match printable ASCII characters
- Extract text between two HTML tags
- Replace all <b> tags with <strong>
- Extract all matches from a regular expression
-
-
C.1 Spacing
- Index
- About the Author
- Copyright
Product information
- Title: Perl One-Liners
- Author(s):
- Release date: December 2013
- Publisher(s): No Starch Press
- ISBN: 9781593275204
You might also like
book
Perl by Example
The World’s Easiest Perl 5 Tutorial—Updated for Today’s Applications and “Modern Perl” Best Practices “When I …
book
Perl Best Practices
Many programmers code by instinct, relying on convenient habits or a "style" they picked up early …
book
Perl Hacks
With more than a million dedicated programmers, Perl has proven to be the best computing language …
book
Beginning Perl
Everything beginners need to start programming with Perl Perl is the ever-popular, flexible, open source programming …