Only two of the seven regex tools on our test box read \d as a digit: grep -P and Perl. GNU sed, gawk, mawk and BusyBox awk all treated it as the letter d, and only gawk warned about it. That is why the best regular expressions books for shell work are not the regex books written for programmers. A pattern copied from a Python chapter can fail silently inside a sed or awk pipeline.
This list ranks eight regex, sed and awk books by what they teach about the engine you will run. It runs from Mastering Regular Expressions to the 2023 second edition of The AWK Programming Language. Every edition, page count and price band was checked against the Amazon catalog. We then timed one pattern in all seven tools on a 1,000,000-line access log. mawk counted the 299,752 server errors fastest at 0.108 s (benchmarked October 2026 on an 8-vCPU Ubuntu 26.04 KVM guest with gawk 5.3.2, not a dedicated lab VM).
Which regex, sed and awk book should you buy?
Buy Mastering Regular Expressions first if regex itself is the gap, and The AWK Programming Language first if the gap is turning text files into reports. The other six books fill narrower holes, and the one to add depends on which tool you spend the most time in:
- Best overall for regex: Mastering Regular Expressions, because its engine chapters explain the flavor differences in our lab.
- Best for awk: The AWK Programming Language, Second Edition, the only title here written after awk learned CSV and UTF-8.
- Best for sed: sed & awk, the classic O’Reilly book on both tools, with a 1997 date to match.
- Best for programmers: Regular Expressions Cookbook, with working patterns in eight languages and no POSIX tools at all.
- Best short start: Learning Regular Expressions by Ben Forta, 144 pages you can finish in a weekend.
None of the eight teaches shell quoting in depth. If you live in Bash rather than in awk, pair one of them with a title from our Bash scripting books list. Quoting is where most broken one-liners actually break, so the two shelves work together.
How fast is each regex engine on a 1,000,000-line log?
mawk and grep -P finished level at about 0.11 s, plain grep -E took 0.154 s, and BusyBox awk was slowest at 0.675 s. Each tool counted lines with an HTTP status from 500 to 599 in a 105 MB synthetic nginx-style access log. Every one returned the same 299,752 matches, so the speed gap is the engine, not the answer. Each command ran three times and the median is shown:
| Tool | Pattern style | Median time | Three runs (s) |
|---|---|---|---|
| mawk | ERE, 5[0-9][0-9] | 0.108 s | 0.108, 0.125, 0.105 |
| grep -P | PCRE, 5\d\d | 0.110 s | 0.157, 0.109, 0.110 |
| grep -E | ERE, 5[0-9]{2} | 0.154 s | 0.154, 0.153, 0.159 |
| perl -ne | Perl, 5\d\d | 0.280 s | 0.280, 0.280, 0.281 |
| gawk | ERE, 5[0-9][0-9] | 0.394 s | 0.278, 0.394, 0.404 |
| sed -nE, piped to wc | ERE, 5[0-9]{2} | 0.451 s | 0.438, 0.458, 0.451 |
| BusyBox awk | ERE, 5[0-9][0-9] | 0.675 s | 0.779, 0.675, 0.662 |
gawk is the noisy row. One of its three runs came in at 0.278 s, so we read its result as 0.28 to 0.40 s, still behind mawk on every run. sed pays twice, once for printing every matching line and again for the pipe to wc -l. That is the cost of a tool with no counter of its own. Matching on the field instead of the whole line ($9 ~ /^5/) did not help either awk. mawk slowed to 0.289 s and gawk held at 0.393 s, because splitting nine fields costs more than one scan of the raw line.
Our first timing pass reported grep -E at a few milliseconds, which is over 15 GB/s and not believable. When GNU grep reads a file and its output goes to /dev/null, it stops at the first match, so a benchmark that discards output measures nothing. That is behaviour we observed in the binary, not an option the grep manual documents. Every grep number above was taken with output written to a file, and the reproduce section at the end shows the trap side by side.
Which tools treat \d as a digit?
Only the Perl-compatible engines do. We fed the string build 42 through a substitution of \d with #. The probe was then repeated with v 42, which has no letter d, to separate “matched a digit” from “matched the letter”:
| Tool | Result on build 42 | Result on v 42 | What \d meant |
|---|---|---|---|
| grep -P | line matched | not tested | a digit |
| perl -pe | build #2 | not tested | a digit |
| grep -E | line matched | no match, exit 1 | the letter d, no warning |
| sed -E | buil# 42 | v 42 unchanged | the letter d, no warning |
| gawk | buil# 42 | not tested | the letter d, with a warning |
| mawk | buil# 42 | v 42 unchanged | the letter d, no warning |
| BusyBox awk | buil# 42 | not tested | the letter d, no warning |
The grep -E row is the dangerous one. It printed the line, so a quick test on real data looks like success, and only the second input shows it never matched a digit at all. gawk was the only POSIX tool that complained, with this warning on stderr:
gawk: cmd. line:1: warning: regexp escape sequence `\d' is not a known regexp operator
The GNU sed manual explains its own behaviour: in sed, \dxxx is a decimal character code, not a class. The portable spelling is [0-9] or [[:digit:]], and a book that writes its patterns that way will work in every row of the table. Our grep vs awk vs sed comparison covers the day-to-day syntax of the three tools if you need it before picking a book.
Which regex features break between grep, sed and awk?
Backreferences, lookahead and CSV parsing split the tools, while interval braces such as a{3} worked in all five POSIX tools we tried. These three results decide which book you need, because each one is covered well by some titles on this list and ignored by others:
- Backreference
(ab)\1onabab: GNU grep -E, GNU sed -E and Perl matched; gawk printed nothing. awk exits 0 whether or not a pattern matches, so the exit code cannot tell a script that it failed. - Lookahead
foo(?=bar): onlygrep -oPreturnedfoo. grep -E warned “? at start of expression” and sed -E refused the pattern. - A quoted comma in CSV:
gawk --csvreturnedDoe, Jane, plaingawk -F,returned"Doe, and mawk and BusyBox rejected--csvas an unknown option.
sed is strict about lookahead and tells you so, which is the better failure. Its error, captured from the same run:
sed: -e expression #1, char 12: Invalid preceding regular expression
The CSV result matters most for the awk books. The --csv option first appeared in 2023 in the one true awk maintained by Brian Kernighan, which is the awk the second edition of The AWK Programming Language documents. gawk adopted it in the 5.3 line, according to the gawk feature history. Any awk book printed before 2023 splits CSV with -F,, which is exactly the version that broke on a name with a comma in it.
Regex books compared at a glance
The flavor column is the one to read first. A book that teaches PCRE or the engine inside a programming language will not warn you about the \d problem above, and a POSIX book will not teach lookahead. Page counts are from the Amazon listings we checked on 28 October 2026:
| Book | Edition, year | Pages | Flavors and tools | Best for |
|---|---|---|---|---|
| Mastering Regular Expressions | 3rd, 2006 | 542 | Engine theory, Perl, Java, .NET, PHP | Understanding why a regex is slow or wrong |
| The AWK Programming Language | 2nd, 2023 | 240 | One true awk with CSV and UTF-8 | awk from first program to little languages |
| sed & awk | 2nd, 1997 | 432 | POSIX sed and awk of the late 1990s | Writing real sed scripts |
| Regular Expressions Cookbook | 2nd, 2012 | 609 | .NET, Java, JavaScript, XRegExp, Perl, PCRE, Python, Ruby | Copyable patterns in application code |
| Effective awk Programming | 4th, 2015 | 560 | GNU awk (gawk) | gawk extensions as a desk reference |
| Learning Regular Expressions | 1st, 2018 | 144 | Generic syntax, app and language notes | A first pass in a weekend |
| Introducing Regular Expressions | 1st, 2012 | 151 | Step-by-step introduction | Absolute beginners |
| Regular Expression Puzzles and AI Coding Assistants | 1st, 2023 | 152 | Python, Copilot, ChatGPT | Practice and checking AI-written regex |
Five of the eight are more than ten years old, and apart from the 2023 puzzle book, no regex title here is newer than 2018. That is less of a problem than it sounds, because POSIX regex and the Perl-style engines have barely moved in that time. The awk side is different, because CSV mode and Unicode-aware string functions are recent. Buy the 2023 awk book, or read the free gawk manual that tracks the current release.
1. Mastering Regular Expressions, Third Edition
Jeffrey Friedl wrote the book that explains our benchmark table, and that is why it sits first.
Chapter 4 covers the mechanics of expression processing: text-directed DFA engines, regex-directed NFA engines, POSIX NFA, and how backtracking decides both the match and the run time. Chapter 6 is a full chapter on crafting efficient expressions. Read together, they give you the model for reading our benchmark table. They also explain why one pattern can return a different match in a POSIX tool and in Perl: POSIX asks for the leftmost-longest match, while Perl takes the first alternative that succeeds. According to the author’s book page, the six general chapters take 282 pages and the rest goes to Perl, Java, .NET and PHP.
Buy it if regex is the skill you want to own rather than borrow, whatever tool you use. Skip it if you want ready-made patterns, because the language chapters are from 2006 (Java 1.5 and 1.6 era) and the book never covers Python or JavaScript in depth. Amazon sold it for roughly $25 to $30 against a $59.99 list price when we checked, listed as low stock, so check the current price first.
2. The AWK Programming Language, Second Edition
Aho, Kernighan and Weinberger rewrote their 1988 book in 2023, and it is the only awk title here that matches what current awk can do.

The 240 pages run from a tutorial through data processing, reports, word processing and little languages, and add a new chapter on exploratory data analysis with Unicode data. The book is written against the one true awk, whose README lists the features it gained for this edition: UTF-8 aware string functions and --csv input with quoted fields. Those features separated gawk from mawk and BusyBox in our CSV test. The CSV examples should run as printed on gawk from the 5.3 line on, and fail on the other two:
- Listing checked: paperback, ISBN 0138269726, 240 pages, sold by Amazon and in stock on 28 October 2026.
- Book site: errata and the full program set are on awk.dev, which saves retyping every example.
- Price band: about $35 to $40, close to the $39.99 list price.
Get it if awk is your report tool or you parse CSV in the shell. Pass on it as a regex book, because none of its nine chapters is about regular expressions, so expect awk usage rather than the depth Friedl gives regex. Pass too if your servers ship mawk as the default awk and you cannot install gawk.
3. sed & awk, Second Edition
Dale Dougherty and Arnold Robbins wrote the book most people mean by “the sed and awk book”. It covers both tools in one O’Reilly volume.
The second edition dates from 1997, and that date sets its limits. It predates every gawk 4.x and 5.x feature and the CSV mode in awk, so the awk half is the weaker half now that the 2023 AWK book exists. For sed alone there is a newer book, Definitive Guide to sed by Daniel A. Goldman (EHDP Press, 2013), which is written for GNU sed; we did not price-check it for this list.
The listing needs a check before you buy. Amazon showed a new copy for about $15 to $20 against a $49.99 list price. The seller was a third party, Golden Pages Books, and the listing gave a later ship date rather than in stock. Buy it if you want both tools explained together in one book; skip it if you only need awk, and read the seller and delivery date on the page rather than assuming next-day shipping.
4. Regular Expressions Cookbook, Second Edition
Jan Goyvaerts and Steven Levithan wrote this for developers who need a pattern that works today in a specific language.

Every recipe is shown for the flavors .NET, Java, JavaScript, XRegExp, Perl, PCRE, Python and Ruby, with code in eight languages including C#, PHP and VB.NET. The recipes cover validation and formatting, numbers, source code and log files, URLs and paths, and markup and data formats. The co-author’s page states that POSIX flavors are left out on purpose. None of it is written for grep -E, sed or awk, and its \d patterns would hit the exact trap in our table.
That makes it the right pick for application code and the wrong one for shell pipelines. The log file recipes still translate to grep -P, which is PCRE, so a sysadmin can use part of it. Amazon listed it at about $25 against a $59.99 list price from a third-party seller, low stock, when we checked.
5. Effective awk Programming, Fourth Edition
Arnold Robbins maintains gawk, and this is the printed form of its manual.
The cover calls it “A GNU Manual”, and the free online gawk manual carries the same title and the same foreword to the fourth edition. The difference is time. The paperback came out in 2015, while the online edition now tracks the 5.4 line and documents --csv, which the printed book cannot. For a gawk user, the free manual is the current reference and the paperback is the readable, bound snapshot of it.
Buy it if you read better on paper and rely on gawk-only features such as arrays of arrays and network I/O, which the manual documents in full. The free online text is newer, so readers happy on a screen lose nothing by passing, and neither do teams whose scripts must also run on mawk. Amazon sold it for about $35 to $40, listed as low stock.
6. Learning Regular Expressions
Ben Forta wrote the shortest route on this list from zero to reading regex that someone else wrote.
Its 144 pages hold eleven short lessons. They run from single characters and sets through repetition, position, subexpressions and backreferences to lookahead, lookbehind and conditions, and end with solutions to common problems. Appendix A shows regex in JavaScript, Java, PHP, Python, .NET, MySQL, Oracle and grep. That helps when one pattern has to move between a database, a script and the shell.
Pick it for a first weekend with regex, or to bring a junior teammate up to speed. A year of daily regex use makes it redundant, since it stops at syntax and does not cover engine internals or performance. Amazon listed it at about $30 from a third-party seller, in stock, when we checked.
7. Introducing Regular Expressions
Michael Fitzgerald wrote the other short introduction, for O’Reilly. The choice between it and Forta comes down to format.
The cover subtitle, “Unraveling Regular Expressions, Step-by-Step”, promises a step-by-step introduction. It came out in August 2012 at 151 pages, so it is six years older than the Forta book and about the same length. We could not reach a table of contents from the publisher during this check, so we rate it only on what we could confirm. Forta has the published lesson list and the language appendix, and that is why it ranks higher. Fitzgerald is the one Amazon sells directly, while the Forta copy we found came from a third-party seller.
Owners of the Forta book should not buy a second beginner title. Amazon sold it for about $25 to $30, low stock, against a $49.99 list price.
8. Regular Expression Puzzles and AI Coding Assistants
David Mertz wrote the practice book on this list. It is also the only one written for a team where an assistant drafts half the patterns.
It sets 24 regex puzzles, gives the author’s own solution to each, and then shows how GitHub Copilot and ChatGPT handled the same puzzle, according to Manning’s page. The code is plain Python. The value is in the comparison. Once you have watched an assistant produce a plausible but wrong pattern, you are better placed to catch a \d in a sed command that an assistant wrote.
Buy it after one of the first two books, as practice. It is a set of 24 puzzles rather than a reference, its code is Python only, and the Manning description never mentions POSIX grep, sed or awk, so shell users get less from it. It ran about $18 from a third-party seller when we checked, against a $39.99 Amazon list price, for 152 pages.
What to check before you buy a regex book
Match the book to the engine you run, not to its reputation. Our lab showed the same pattern meaning three different things across seven tools, so the questions below matter more than page count or star rating:
- Flavor: POSIX BRE and ERE (grep, sed, awk) or Perl-style (grep -P, Perl, Python, JavaScript). The Cookbook is Perl-style only; sed & awk is POSIX only.
- awk implementation: run
awk --versionbefore buying an awk book. On our Ubuntu boxawkpointed to gawk, but many minimal images ship mawk or BusyBox, and neither accepted--csv. - Print date against feature date: CSV mode and UTF-8 aware awk arrived in 2023, so any earlier awk book splits CSV the way that broke in our test.
- Character classes: a book that writes
[[:digit:]]and[0-9]produces patterns that work everywhere; one that writes\dand\wassumes PCRE.
If you are studying for an exam rather than buying for work, the regex objectives in Linux certifications are POSIX grep and sed. Our LPIC-1 regular expressions guide and the text stream filters lesson cover that scope for free, and the CompTIA Linux+ books list covers the study guides that include it.
Reproduce the regex benchmark on your own machine
The whole test takes about two minutes on a Linux box with gawk and mawk installed (sudo apt install gawk mawk on Debian or Ubuntu), and running it on your own servers tells you which awk you really have. These are the steps we ran, in order.
Step 1: Record the tool versions
Version numbers matter because mawk and gawk behave differently on --csv, so print them first:
gawk --version | head -1
mawk -W version 2>&1 | head -1
sed --version | head -1
grep --version | head -1
Our box printed these four lines:
GNU Awk 5.3.2, API 4.0, PMA Avon 8-g1, (GNU MPFR 4.2.2, GNU MP 6.3.0)
mawk 1.3.4 20260129
sed (GNU sed) 4.9
grep (GNU grep) 3.12
Keep these lines with your timings, because a different gawk or mawk build will move the numbers more than the hardware does.
Step 2: Generate the 1,000,000-line log
A fixed random seed (42) makes the file repeatable, so with the same gawk version your match count should equal ours exactly:
gawk 'BEGIN{srand(42); split("200 200 200 200 301 304 404 500 502 503",st," "); split("GET POST PUT DELETE",m," "); for(i=1;i<=1000000;i++){ip=int(rand()*223)+1"."int(rand()*255)"."int(rand()*255)"."int(rand()*254)+1; printf "%s - - [28/Oct/2026:10:%02d:%02d +0000] \"%s /api/v1/item/%d HTTP/1.1\" %s %d \"-\" \"curl/8.18.0\"\n", ip, int(rand()*60), int(rand()*60), m[int(rand()*4)+1], int(rand()*99999), st[int(rand()*10)+1], int(rand()*50000)}}' > access.log
The file came out at 105 MB, and its first two lines look like a normal nginx access log:
55.101.243.44 - - [28/Oct/2026:10:35:56 +0000] "DELETE /api/v1/item/74933 HTTP/1.1" 200 30936 "-" "curl/8.18.0"
41.77.225.6 - - [28/Oct/2026:10:10:48 +0000] "GET /api/v1/item/88576 HTTP/1.1" 404 10354 "-" "curl/8.18.0"
Status 500, 502 and 503 make up three of the ten weighted status values, which is why roughly 30 percent of the lines match.
Step 3: Time grep without the /dev/null trap
Run the same count twice, once discarding output and once writing it to a file, to see why the first number cannot be trusted:
TIMEFORMAT="real %Rs"
time grep -cE '" 5[0-9]{2} ' access.log > /dev/null
time grep -cE '" 5[0-9]{2} ' access.log > count.txt
cat count.txt
The /dev/null run finished 27 times faster because grep stopped at the first match, while the file run read all 105 MB and counted every line. This was a separate run from the benchmark table, which is why it shows 0.164 s rather than the 0.154 s median:
real 0.006s
real 0.164s
299752
Use the second form for every grep timing, including inside benchmark scripts that would otherwise send output to /dev/null.
Step 4: Compare the awk implementations
Time mawk and gawk on the same pattern, repeating each a few times because the first run warms the page cache:
time mawk '/" 5[0-9][0-9] /{n++} END{print n}' access.log > count.txt
time gawk '/" 5[0-9][0-9] /{n++} END{print n}' access.log > count.txt
Both should write 299752 to count.txt. On our box mawk held between 0.105 and 0.125 s across three runs, and gawk between 0.278 and 0.404 s.
Step 5: Probe \d and CSV handling
These two checks decide which kind of regex book suits the machine. The first shows whether \d is a digit; the second shows whether your awk parses quoted CSV fields:
echo 'build 42' | sed -E 's/\d/#/'
printf '1001,"Doe, Jane",ops\n' | awk --csv '{print $2}'
On our box awk pointed to gawk, so GNU sed replaced the letter d and awk returned the whole quoted name, comma included:
buil# 42
Doe, Jane
If the second command prints mawk: not an option: --csv or a BusyBox usage screen instead, the awk on that machine has no CSV mode. Install gawk before working through the CSV chapters of the second edition, or keep the data on that machine free of quoted commas.





