Acknowledgments |
|
xvii | |
1 Introduction To Perl One-Liners |
|
1 | (6) |
2 Spacing |
|
7 | (10) |
|
|
7 | (4) |
|
2.2 Double-space a file, excluding the blank lines |
|
|
11 | (1) |
|
|
11 | (1) |
|
|
12 | (1) |
|
2.5 Add a blank line before every line |
|
|
12 | (1) |
|
2.6 Remove all blank lines |
|
|
12 | (2) |
|
2.7 Remove all consecutive blank lines, leaving only one |
|
|
14 | (1) |
|
2.8 Compress/expand all blank lines into N consecutive lines |
|
|
14 | (1) |
|
2.9 Double-space between all words |
|
|
15 | (1) |
|
2.10 Remove all spacing between words |
|
|
15 | (1) |
|
2.11 Change all spacing between words to one space |
|
|
16 | (1) |
|
2.12 Insert a space between all characters |
|
|
16 | (1) |
3 Numbering |
|
17 | (12) |
|
3.1 Number all lines in a file |
|
|
17 | (1) |
|
3.2 Number only non-empty lines in a file |
|
|
18 | (1) |
|
3.3 Number and print only non-empty lines in a file (drop empty lines) |
|
|
19 | (1) |
|
3.4 Number all lines but print line numbers only for non-empty lines |
|
|
20 | (1) |
|
3.5 Number only lines that match a pattern; print others unmodified |
|
|
20 | (1) |
|
3.6 Number and print only lines that match a pattern |
|
|
21 | (1) |
|
3.7 Number all lines but print line numbers only for lines that match a pattern |
|
|
21 | (1) |
|
3.8 Number all lines in a file using a custom format |
|
|
22 | (1) |
|
3.9 Print the total number of lines in a file (emulate we -l) |
|
|
22 | (2) |
|
3.10 Print the number of non-empty lines in a file |
|
|
24 | (1) |
|
3.11 Print the number of empty lines in a file |
|
|
25 | (1) |
|
3.12 Print the number of lines in a file that match a pattern (emulate grep -c) |
|
|
25 | (1) |
|
3.13 Number words across all lines |
|
|
26 | (1) |
|
3.14 Number words on each individual line |
|
|
26 | (1) |
|
3.15 Replace all words with their numeric positions |
|
|
27 | (2) |
4 Calculations |
|
29 | (20) |
|
4.1 Check if a number is a prime |
|
|
29 | (1) |
|
4.2 Print the sum of all fields on each line |
|
|
30 | (1) |
|
4.3 Print the sum of all fields on all lines |
|
|
31 | (1) |
|
4.4 Shuffle all fields on each line |
|
|
32 | (1) |
|
4.5 Find the numerically smallest element (minimum element) on each line |
|
|
33 | (1) |
|
4.6 Find the numerically smallest element (minimum element) over all lines |
|
|
33 | (2) |
|
4.7 Find the numerically largest element (maximum element) on each line |
|
|
35 | (1) |
|
4.8 Find the numerically largest element (maximum element) over all lines |
|
|
35 | (1) |
|
4.9 Replace each field with its absolute value |
|
|
36 | (1) |
|
4.10 Print the total number of fields on each line |
|
|
36 | (1) |
|
4.11 Print the total number of fields on each line, followed by the line |
|
|
37 | (1) |
|
4.12 Print the total number of fields on all lines |
|
|
37 | (1) |
|
4.13 Print the total number of fields that match a pattern |
|
|
38 | (1) |
|
4.14 Print the total number of lines that match a pattern |
|
|
38 | (1) |
|
|
39 | (1) |
|
|
39 | (1) |
|
4.17 Print UNIX time (seconds since January 1, 1970, 00:00:00 UTC) |
|
|
39 | (1) |
|
4.18 Print Greenwich Mean Time and local computer time |
|
|
40 | (1) |
|
4.19 Print yesterday's date |
|
|
41 | (1) |
|
4.20 Print the date 14 months, 9 days, and 7 seconds ago |
|
|
41 | (1) |
|
4.21 Calculate the factorial |
|
|
41 | (1) |
|
4.22 Calculate the greatest common divisor |
|
|
42 | (1) |
|
4.23 Calculate the least common multiple |
|
|
43 | (1) |
|
4.24 Generate 10 random numbers between 5 and 15 (excluding 15) |
|
|
43 | (1) |
|
4.25 Generate all permutations of a list |
|
|
44 | (1) |
|
4.26 Generate the powerset |
|
|
45 | (1) |
|
4.27 Convert an IP address to an unsigned integer |
|
|
45 | (2) |
|
4.28 Convert an unsigned integer to an IP address |
|
|
47 | (2) |
5 Working With Arrays And Strings |
|
49 | (10) |
|
5.1 Generate and print the alphabet |
|
|
49 | (1) |
|
5.2 Generate and print all the strings from "a" to "zz" |
|
|
50 | (1) |
|
5.3 Create a hex lookup table |
|
|
51 | (1) |
|
5.4 Generate a random eight-character password |
|
|
51 | (1) |
|
5.5 Create a string of specific length |
|
|
52 | (1) |
|
5.6 Create an array from a string |
|
|
52 | (1) |
|
5.7 Create a string from the command-line arguments |
|
|
53 | (1) |
|
5.8 Find the numeric values for characters in a string |
|
|
53 | (2) |
|
5.9 Convert a list of numeric ASCII values into a string |
|
|
55 | (1) |
|
5.10 Generate an array with odd numbers from 1 to 100 |
|
|
55 | (1) |
|
5.11 Generate an array with even numbers from 1 to 100 |
|
|
56 | (1) |
|
5.12 Find the length of a string |
|
|
56 | (1) |
|
5.13 Find the number of elements in an array |
|
|
56 | (3) |
6 Text Conversion And Substitution |
|
59 | (10) |
|
|
59 | (1) |
|
6.2 Base64-encode a string |
|
|
60 | (1) |
|
6.3 Base64-decode a string |
|
|
61 | (1) |
|
|
61 | (1) |
|
6.5 URL-unescape a string |
|
|
61 | (1) |
|
|
62 | (1) |
|
|
62 | (1) |
|
6.8 Convert all text to uppercase |
|
|
62 | (1) |
|
6.9 Convert all text to lowercase |
|
|
62 | (1) |
|
6.10 Uppercase only the first letter of each line |
|
|
63 | (1) |
|
6.11 Invert the letter case |
|
|
63 | (1) |
|
6.12 Title-case each line |
|
|
63 | (1) |
|
6.13 Strip leading whitespace (spaces, tabs) from the beginning of each line |
|
|
64 | (1) |
|
6.14 Strip trailing whitespace (spaces, tabs) from the end of each line |
|
|
64 | (1) |
|
6.15 Strip whitespace (spaces, tabs) from the beginning and end of each line |
|
|
64 | (1) |
|
6.16 Convert UNIX newlines to DOS/Windows newlines |
|
|
65 | (1) |
|
6.17 Convert DOS/Windows newlines to UNIX newlines |
|
|
65 | (1) |
|
6.18 Convert UNIX newlines to Mac newlines |
|
|
65 | (1) |
|
6.19 Substitute (find and replace) "foo" with "bar" on each line |
|
|
66 | (1) |
|
6.20 Substitute (find and replace) "foo" with "bar" on lines that match "baz" |
|
|
66 | (1) |
|
6.21 Print paragraphs in reverse order |
|
|
66 | (1) |
|
6.22 Print all lines in reverse order |
|
|
67 | (1) |
|
6.23 Print columns in reverse order |
|
|
67 | (2) |
7 Selectively Printing And Deleting Lines |
|
69 | (14) |
|
7.1 Print the first line of a file (emulate head -1) |
|
|
70 | (1) |
|
7.2 Print the first 10 lines of a file (emulate head -10) |
|
|
70 | (1) |
|
7.3 Print the last line of a file (emulate tail -1) |
|
|
71 | (1) |
|
7.4 Print the last 10 lines of a file (emulate tail -10) |
|
|
72 | (1) |
|
7.5 Print only lines that match a regular expression |
|
|
72 | (1) |
|
7.6 Print only lines that do not match a regular expression |
|
|
73 | (1) |
|
7.7 Print every line preceding a line that matches a regular expression |
|
|
73 | (1) |
|
7.8 Print every line following a line that matches a regular expression |
|
|
74 | (1) |
|
7.9 Print lines that match regular expressions AAA and BBB in any order |
|
|
75 | (1) |
|
7.10 Print lines that don't match regular expressions AAA and BBB |
|
|
75 | (1) |
|
7.11 Print lines that match regular expression AAA followed by BBB followed by CCC |
|
|
75 | (1) |
|
7.12 Print lines that are at least 80 characters long |
|
|
76 | (1) |
|
7.13 Print lines that are fewer than 80 characters long |
|
|
76 | (1) |
|
|
76 | (1) |
|
7.15 Print all lines except line 27 |
|
|
76 | (1) |
|
7.16 Print only lines 13, 19, and 67 |
|
|
77 | (1) |
|
7.17 Print all lines from 17 to 30 |
|
|
77 | (1) |
|
7.18 Print all lines between two regular expressions (including the lines that match) |
|
|
78 | (1) |
|
7.19 Print the longest line |
|
|
78 | (1) |
|
7.20 Print the shortest line |
|
|
79 | (1) |
|
7.21 Print all lines containing digits |
|
|
79 | (1) |
|
7.22 Print all lines containing only digits |
|
|
79 | (1) |
|
7.23 Print all lines containing only alphabetic characters |
|
|
80 | (1) |
|
7.24 Print every second line |
|
|
80 | (1) |
|
7.25 Print every second line, beginning with the second line |
|
|
80 | (1) |
|
7.26 Print all repeated lines only once |
|
|
81 | (1) |
|
7.27 Print all unique lines |
|
|
81 | (2) |
8 Useful Regular Expressions |
|
83 | (12) |
|
8.1 Match something that looks like an IP address |
|
|
83 | (1) |
|
8.2 Test whether a number is in the range 0 to 255 |
|
|
84 | (1) |
|
|
85 | (1) |
|
8.4 Check whether a string looks like an email address |
|
|
86 | (1) |
|
8.5 Check whether a string is a number |
|
|
87 | (1) |
|
8.6 Check whether a word appears in a string twice |
|
|
88 | (1) |
|
8.7 Increase all integers in a string by one |
|
|
89 | (1) |
|
8.8 Extract the HTTP User-Agent string from HTTP headers |
|
|
89 | (1) |
|
8.9 Match printable ASCII characters |
|
|
90 | (1) |
|
8.10 Extract text between two HTML tags |
|
|
90 | (1) |
|
8.11 Replace all <b> tags with <strong> |
|
|
91 | (1) |
|
8.12 Extract all matches from a regular expression |
|
|
92 | (3) |
A PERL'S Special Variables |
|
95 | (10) |
|
|
95 | (4) |
|
Using $_ with the -n argument |
|
|
96 | (1) |
|
Using $_ with the -p argument |
|
|
97 | (1) |
|
|
98 | (1) |
|
|
99 | (1) |
|
|
100 | (1) |
|
|
101 | (1) |
|
A.5 Variables $1, $2, $3, and so on |
|
|
101 | (1) |
|
|
102 | (1) |
|
|
102 | (1) |
|
|
103 | (1) |
|
|
103 | (1) |
|
|
104 | (1) |
B Using PERL One-Liners On Windows |
|
105 | (12) |
|
|
105 | (1) |
|
|
106 | (1) |
|
B.3 Perl One-Liners in Windows Bash |
|
|
107 | (1) |
|
B.4 Perl One-Liners in the Windows Command Prompt |
|
|
108 | (3) |
|
Converting One-Liners in the Windows Command Prompt |
|
|
108 | (2) |
|
|
110 | (1) |
|
|
111 | (1) |
|
B.5 Perl One-Liners in PowerShell |
|
|
111 | (6) |
|
Converting One-Liners in PowerShell |
|
|
112 | (2) |
|
One-Liners in PowerShell 3.0+ |
|
|
114 | (3) |
C PER1LINE.TXT |
|
117 | (22) |
|
|
117 | (2) |
|
|
119 | (2) |
|
|
121 | (6) |
|
C.4 Working with Arrays and Strings |
|
|
127 | (3) |
|
C.5 Text Conversion and Substitution |
|
|
130 | (3) |
|
C.6 Selectively Printing and Deleting Lines |
|
|
133 | (3) |
|
C.7 Useful Regular Expressions |
|
|
136 | (3) |
Index |
|
139 | |