Cover image for Perl One-Liners

Book description

Perl One-Liners showcases 130 short and compelling lines of code that do all sorts of handy, geeky things.

Table of Contents

  1. About the Author
  2. About the Technical Reviewer
  3. Acknowledgments
  4. 1. Introduction to Perl One-Liners
  5. 2. Spacing
    1. 2.1 Double-space a file
    2. 2.2 Double-space a file, excluding the blank lines
    3. 2.3 Triple-space a file
    4. 2.4 N-space a file
    5. 2.5 Add a blank line before every line
    6. 2.6 Remove all blank lines
    7. 2.7 Remove all consecutive blank lines, leaving only one
    8. 2.8 Compress/expand all blank lines into N consecutive lines
    9. 2.9 Double-space between all words
    10. 2.10 Remove all spacing between words
    11. 2.11 Change all spacing between words to one space
    12. 2.12 Insert a space between all characters
  6. 3. Numbering
    1. 3.1 Number all lines in a file
    2. 3.2 Number only non-empty lines in a file
    3. 3.3 Number and print only non-empty lines in a file (drop empty lines)
    4. 3.4 Number all lines but print line numbers only for non-empty lines
    5. 3.5 Number only lines that match a pattern; print others unmodified
    6. 3.6 Number and print only lines that match a pattern
    7. 3.7 Number all lines but print line numbers only for lines that match a pattern
    8. 3.8 Number all lines in a file using a custom format
    9. 3.9 Print the total number of lines in a file (emulate wc -l)
    10. 3.10 Print the number of non-empty lines in a file
    11. 3.11 Print the number of empty lines in a file
    12. 3.12 Print the number of lines in a file that match a pattern (emulate grep -c)
    13. 3.13 Number words across all lines
    14. 3.14 Number words on each individual line
    15. 3.15 Replace all words with their numeric positions
  7. 4. Calculations
    1. 4.1 Check if a number is a prime
    2. 4.2 Print the sum of all fields on each line
    3. 4.3 Print the sum of all fields on all lines
    4. 4.4 Shuffle all fields on each line
    5. 4.5 Find the numerically smallest element (minimum element) on each line
    6. 4.6 Find the numerically smallest element (minimum element) over all lines
    7. 4.7 Find the numerically largest element (maximum element) on each line
    8. 4.8 Find the numerically largest element (maximum element) over all lines
    9. 4.9 Replace each field with its absolute value
    10. 4.10 Print the total number of fields on each line
    11. 4.11 Print the total number of fields on each line, followed by the line
    12. 4.12 Print the total number of fields on all lines
    13. 4.13 Print the total number of fields that match a pattern
    14. 4.14 Print the total number of lines that match a pattern
    15. 4.15 Print the number π
    16. 4.16 Print the number
    17. 4.17 Print UNIX time (seconds since January 1, 1970, 00:00:00 UTC)
    18. 4.18 Print Greenwich Mean Time and local computer time
    19. 4.19 Print yesterday’s date
    20. 4.20 Print the date 14 months, 9 days, and 7 seconds ago
    21. 4.21 Calculate the factorial
    22. 4.22 Calculate the greatest common divisor
    23. 4.23 Calculate the least common multiple
    24. 4.24 Generate 10 random numbers between 5 and 15 (excluding 15)
    25. 4.25 Generate all permutations of a list
    26. 4.26 Generate the powerset
    27. 4.27 Convert an IP address to an unsigned integer
    28. 4.28 Convert an unsigned integer to an IP address
  8. 5. Working With Arrays and Strings
    1. 5.1 Generate and print the alphabet
    2. 5.2 Generate and print all the strings from “a” to “zz”
    3. 5.3 Create a hex lookup table
    4. 5.4 Generate a random eight-character password
    5. 5.5 Create a string of specific length
    6. 5.6 Create an array from a string
    7. 5.7 Create a string from the command-line arguments
    8. 5.8 Find the numeric values for characters in a string
    9. 5.9 Convert a list of numeric ASCII values into a string
    10. 5.10 Generate an array with odd numbers from 1 to 100
    11. 5.11 Generate an array with even numbers from 1 to 100
    12. 5.12 Find the length of a string
    13. 5.13 Find the number of elements in an array
  9. 6. Text Conversion and Substitution
    1. 6.1 ROT13 a string
    2. 6.2 Base64-encode a string
    3. 6.3 Base64-decode a string
    4. 6.4 URL-escape a string
    5. 6.5 URL-unescape a string
    6. 6.6 HTML-encode a string
    7. 6.7 HTML-decode a string
    8. 6.8 Convert all text to uppercase
    9. 6.9 Convert all text to lowercase
    10. 6.10 Uppercase only the first letter of each line
    11. 6.11 Invert the letter case
    12. 6.12 Title-case each line
    13. 6.13 Strip leading whitespace (spaces, tabs) from the beginning of each line
    14. 6.14 Strip trailing whitespace (spaces, tabs) from the end of each line
    15. 6.15 Strip whitespace (spaces, tabs) from the beginning and end of each line
    16. 6.16 Convert UNIX newlines to DOS/Windows newlines
    17. 6.17 Convert DOS/Windows newlines to UNIX newlines
    18. 6.18 Convert UNIX newlines to Mac newlines
    19. 6.19 Substitute (find and replace) “foo” with “bar” on each line
    20. 6.20 Substitute (find and replace) “foo” with “bar” on lines that match “baz”
    21. 6.21 Print paragraphs in reverse order
    22. 6.22 Print all lines in reverse order
    23. 6.23 Print columns in reverse order
  10. 7. Selectively Printing and Deleting Lines
    1. 7.1 Print the first line of a file (emulate head -1)
    2. 7.2 Print the first 10 lines of a file (emulate head -10)
    3. 7.3 Print the last line of a file (emulate tail -1)
    4. 7.4 Print the last 10 lines of a file (emulate tail -10)
    5. 7.5 Print only lines that match a regular expression
    6. 7.6 Print only lines that do not match a regular expression
    7. 7.7 Print every line preceding a line that matches a regular expression
    8. 7.8 Print every line following a line that matches a regular expression
    9. 7.9 Print lines that match regular expressions AAA and BBB in any order
    10. 7.10 Print lines that don’t match regular expressions AAA and BBB
    11. 7.11 Print lines that match regular expression AAA followed by BBB followed by CCC
    12. 7.12 Print lines that are at least 80 characters long
    13. 7.13 Print lines that are fewer than 80 characters long
    14. 7.14 Print only line 13
    15. 7.15 Print all lines except line 27
    16. 7.16 Print only lines 13, 19, and 67
    17. 7.17 Print all lines from 17 to 30
    18. 7.18 Print all lines between two regular expressions (including the lines that match)
    19. 7.19 Print the longest line
    20. 7.20 Print the shortest line
    21. 7.21 Print all lines containing digits
    22. 7.22 Print all lines containing only digits
    23. 7.23 Print all lines containing only alphabetic characters
    24. 7.24 Print every second line
    25. 7.25 Print every second line, beginning with the second line
    26. 7.26 Print all repeated lines only once
    27. 7.27 Print all unique lines
  11. 8. Useful Regular Expressions
    1. 8.1 Match something that looks like an IP address
    2. 8.2 Test whether a number is in the range 0 to 255
    3. 8.3 Match an IP address
    4. 8.4 Check whether a string looks like an email address
    5. 8.5 Check whether a string is a number
    6. 8.6 Check whether a word appears in a string twice
    7. 8.7 Increase all integers in a string by one
    8. 8.8 Extract the HTTP User-Agent string from HTTP headers
    9. 8.9 Match printable ASCII characters
    10. 8.10 Extract text between two HTML tags
    11. 8.11 Replace all <b> tags with <strong>
    12. 8.12 Extract all matches from a regular expression
  12. A. Perl’s Special Variables
    1. A.1 Variable $_
      1. Using $_ with the -n argument
      2. Using $_ with the -p argument
      3. Using $_ explicitly
    2. A.2 Variable $.
    3. A.3 Variable $/
    4. A.4 Variable $\
    5. A.5 Variables $1, $2, $3, and so on
    6. A.6 Variable $,
    7. A.7 Variable $”
    8. A.8 Variable @F
    9. A.9 Variable @ARGV
    10. A.10 Variable %ENV
  13. B. Using Perl One-Liners On Windows
    1. B.1 Perl on Windows
    2. B.2 Bash on Windows
    3. B.3 Perl One-Liners in Windows Bash
    4. B.4 Perl One-Liners in the Windows Command Prompt
      1. Converting One-Liners in the Windows Command Prompt
      2. Symbol Challenges
      3. Windows File Paths
    5. B.5 Perl One-Liners in PowerShell
      1. Converting One-Liners in PowerShell
      2. One-Liners in PowerShell 3.0+
  14. C. Perl1Line.Txt
    1. C.1 Spacing
      1.  
        1. Double-space a file
        2. Double-space a file, excluding the blank lines
        3. Triple-space a file
        4. N-space a file
        5. Add a blank line before every line
        6. Remove all blank lines
        7. Remove all consecutive blank lines, leaving only one
        8. Compress/expand all blank lines into N consecutive lines
        9. Double-space between all words
        10. Remove all spacing between words
        11. Change all spacing between words to one space
        12. Insert a space between all characters
    2. C.2 Numbering
      1.  
        1. Number all lines in a file
        2. Number only non-empty lines in a file
        3. Number and print only non-empty lines in a file (drop empty lines)
        4. Number all lines but print line numbers only for non-empty lines
        5. Number only lines that match a pattern; print others unmodified
        6. Number and print only lines that match a pattern
        7. Number all lines but print line numbers only for lines that match a pattern
        8. Number all lines in a file using a custom format
        9. Print the total number of lines in a file (emulate wc -l)
        10. Print the number of non-empty lines in a file
        11. Print the number of empty lines in a file
        12. Print the number of lines in a file that match a pattern (emulate grep -c)
        13. Number words across all lines
        14. Number words on each individual line
        15. Replace all words with their numeric positions
    3. C.3 Calculations
      1.  
        1. Check if a number is a prime
        2. Print the sum of all fields on each line
        3. Print the sum of all fields on all lines
        4. Shuffle all fields on each line
        5. Find the numerically smallest element (minimum element) on each line
        6. Find the numerically smallest element (minimum element) over all lines
        7. Find the numerically largest element (maximum element) on each line
        8. Find the numerically largest element (maximum element) over all lines
        9. Replace each field with its absolute value
        10. Print the total number of fields on each line
        11. Print the total number of fields on each line, followed by the line
        12. Print the total number of fields on all lines
        13. Print the total number of fields that match a pattern
        14. Print the total number of lines that match a pattern
        15. Print the number π
        16. Print the number
        17. Print UNIX time (seconds since January 1, 1970, 00:00:00 UTC)
        18. Print Greenwich Mean Time and local computer time
        19. Print yesterday’s date
        20. Print the date 14 months, 9 days, and 7 seconds ago
        21. Calculate the factorial
        22. Calculate the greatest common divisor
        23. Calculate the least common multiple
        24. Generate 10 random numbers between 5 and 15 (excluding 15)
        25. Generate all permutations of a list
        26. Generate the powerset
        27. Convert an IP address to an unsigned integer
        28. Convert an unsigned integer to an IP address
    4. C.4 Working with Arrays and Strings
      1.  
        1. Generate and print the alphabet
        2. Generate and print all the strings from “a” to “zz”
        3. Create a hex lookup table
        4. Generate a random eight-character password
        5. Create a string of specific length
        6. Create an array from a string
        7. Create a string from the command-line arguments
        8. Find the numeric values for characters in a string
        9. Convert a list of numeric ASCII values into a string
        10. Generate an array with odd numbers from 1 to 100
        11. Generate an array with even numbers from 1 to 100
        12. Find the length of a string
        13. Find the number of elements in an array
    5. C.5 Text Conversion and Substitution
      1.  
        1. ROT13 a string
        2. Base64-encode a string
        3. Base64-decode a string
        4. URL-escape a string
        5. URL-unescape a string
        6. HTML-encode a string
        7. HTML-decode a string
        8. Convert all text to uppercase
        9. Convert all text to lowercase
        10. Uppercase only the first letter of each line
        11. Invert the letter case
        12. Title-case each line
        13. Strip leading whitespace (spaces, tabs) from the beginning of each line
        14. Strip trailing whitespace (spaces, tabs) from the end of each line
        15. Strip whitespace (spaces, tabs) from the beginning and end of each line
        16. Convert UNIX newlines to DOS/Windows newlines
        17. Convert DOS/Windows newlines to UNIX newlines
        18. Convert UNIX newlines to Mac newlines
        19. Substitute (find and replace) “foo” with “bar” on each line
        20. Substitute (find and replace) “foo” with “bar” on lines that match “baz”
        21. Print paragraphs in reverse order
        22. Print all lines in reverse order
        23. Print columns in reverse order
    6. C.6 Selectively Printing and Deleting Lines
      1.  
        1. Print the first line of a file (emulate head -1)
        2. Print the first 10 lines of a file (emulate head -10)
        3. Print the last line of a file (emulate tail -1)
        4. Print the last 10 lines of a file (emulate tail -10)
        5. Print only lines that match a regular expression
        6. Print only lines that do not match a regular expression
        7. Print every line preceding a line that matches a regular expression
        8. Print every line following a line that matches a regular expression
        9. Print lines that match regular expressions AAA and BBB in any order
        10. Print lines that don’t match regular expressions AAA and BBB
        11. Print lines that match regular expression AAA followed by BBB followed by CCC
        12. Print lines that are at least 80 characters long
        13. Print lines that are fewer than 80 characters long
        14. Print only line 13
        15. Print all lines except line 27
        16. Print only lines 13, 19, and 67
        17. Print all lines from 17 to 30
        18. Print all lines between two regular expressions (including the lines that match)
        19. Print the longest line
        20. Print the shortest line
        21. Print all lines containing digits
        22. Print all lines containing only digits
        23. Print all lines containing only alphabetic characters
        24. Print every second line
        25. Print every second line, beginning with the second line
        26. Print all repeated lines only once
        27. Print all unique lines
    7. C.7 Useful Regular Expressions
      1.  
        1. Match something that looks like an IP address
        2. Test whether a number is in the range 0 to 255
        3. Match an IP address
        4. Check whether a string looks like an email address
        5. Check whether a string is a number
        6. Check whether a word appears in a string twice
        7. Increase all integers in a string by one
        8. Extract the HTTP User-Agent string from HTTP headers
        9. Match printable ASCII characters
        10. Extract text between two HTML tags
        11. Replace all <b> tags with <strong>
        12. Extract all matches from a regular expression
  15. Index
  16. About the Author
  17. Copyright