Perl one-liners : 130 programs that get things done

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

"Snappy Perl programs to streamline tasks and sharpen coding skills"-- Provided by publisher.

Author(s): Peteris Krumins
Publisher: No Starch Press
Year: 2014

Language: English
Pages: xv,146
City: San Francisco, CA

About the Author
About the Technical Reviewer
Acknowledgments
Chapter 1: Introduction to Perl One-Liners
Chapter 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
Chapter 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
Chapter 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
Chapter 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
Chapter 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
Chapter 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
Chapter 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 tags with
8.12 Extract all matches from a regular expression
Appendix A: Perl’s Special Variables
A.1 Variable $_
Using $_ with the -n argument
Using $_ with the -p argument
Using $_ explicitly
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
Appendix B: 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
Converting One-Liners in the Windows Command Prompt
Symbol Challenges
Windows File Paths
B.5 Perl One-Liners in PowerShell
Converting One-Liners in PowerShell
One-Liners in PowerShell 3.0+
Appendix C: 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
Index
Blank Page