Skip to content

Commit b49fd35

Browse files
updates for version 2.0
1 parent 9a98b69 commit b49fd35

55 files changed

Lines changed: 5162 additions & 3615 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎LICENSE‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Sundeep Agarwal
3+
Copyright (c) 2023 Sundeep Agarwal
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

‎README.md‎

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# GNU GREP and RIPGREP
1+
# CLI text processing with GNU grep and ripgrep
22

3-
Example based guide to mastering GNU grep and ripgrep.
3+
Example based guide to mastering GNU grep and ripgrep. Visit https://youtu.be/MSbGokwHm-A for a short video about the book.
44

5-
<p align="center">
6-
<img src="./images/grep.png" width="320px" height="400px" />
7-
</p>
5+
<p align="center"><img src="./images/grep_ls.png" alt="CLI text processing with GNU grep and ripgrep ebook cover image" /></p>
86

9-
The book also includes exercises to test your understanding, which are presented together as a single file in this repo - [Exercises.md](./exercises/Exercises.md)
7+
The book also includes exercises to test your understanding, which are presented together as a single file in this repo [Exercises.md](./exercises/Exercises.md)
108

119
For solutions to the exercises, see [Exercise_solutions.md](./exercises/Exercise_solutions.md).
1210

@@ -18,37 +16,35 @@ See [Version_changes.md](./Version_changes.md) to keep track of changes made to
1816

1917
# E-book
2018

21-
You can purchase the pdf/epub versions of the book using these links:
19+
* You can purchase the pdf/epub versions of the book using these links:
20+
* https://learnbyexample.gumroad.com/l/gnugrep_ripgrep
21+
* https://leanpub.com/gnugrep_ripgrep
22+
* You can also get the book as part of these bundles:
23+
* **All books bundle** bundle from https://learnbyexample.gumroad.com/l/all-books
24+
* Includes all my programming books
25+
* **Magical one-liners** bundle from https://learnbyexample.gumroad.com/l/oneliners or https://leanpub.com/b/oneliners
26+
* **Awesome Regex** bundle from https://learnbyexample.gumroad.com/l/regex or https://leanpub.com/b/regex
27+
* See https://learnbyexample.github.io/books/ for a list of other books
2228

23-
* https://learnbyexample.gumroad.com/l/gnugrep_ripgrep
24-
* https://leanpub.com/gnugrep_ripgrep
25-
26-
You can also get the book as part of these bundles:
27-
28-
* **Magical one-liners** bundle from https://learnbyexample.gumroad.com/l/oneliners or https://leanpub.com/b/oneliners
29-
* **Awesome Regex** bundle from https://learnbyexample.gumroad.com/l/regex or https://leanpub.com/b/regex
30-
* **All books bundle** bundle from https://learnbyexample.gumroad.com/l/all-books
31-
* Includes all my programming books
32-
33-
See https://learnbyexample.github.io/books/ for a list of other books
34-
35-
For a preview of the book, see [sample chapters](https://github.com/learnbyexample/learn_gnugrep_ripgrep/blob/master/sample_chapters/gnu_grep_sample.pdf)
29+
For a preview of the book, see [sample chapters](./sample_chapters/gnu_grep_sample.pdf).
3630

3731
The book can also be [viewed as a single markdown file in this repo](./gnu_grep.md). See my blogpost on [generating pdfs from markdown using pandoc](https://learnbyexample.github.io/tutorial/ebook-generation/customizing-pandoc/) if you are interested in the ebook creation process.
3832

3933
For the web version of the book, visit https://learnbyexample.github.io/learn_gnugrep_ripgrep/
4034

4135
<br>
4236

43-
# Feedback
37+
# Feedback and Contributing
4438

45-
[Open an issue](https://github.com/learnbyexample/learn_gnugrep_ripgrep/issues) if you spot any typo/errors.
39+
⚠️ ⚠️ Please DO NOT submit pull requests. Main reason being any modification requires changes in multiple places.
4640

47-
:warning: :warning: Please DO NOT submit pull requests. Main reason being any modification requires changes in multiple places.
41+
I would highly appreciate if you'd let me know how you felt about this book. It could be anything from a simple thank you, pointing out a typo, mistakes in code snippets, which aspects of the book worked for you (or didn't!) and so on. Reader feedback is essential and especially so for self-published authors.
4842

49-
I'd also highly appreciate your feedback about the book.
43+
You can reach me via:
5044

51-
Twitter: https://twitter.com/learn_byexample
45+
* Issue Manager: [https://github.com/learnbyexample/learn_gnugrep_ripgrep/issues](https://github.com/learnbyexample/learn_gnugrep_ripgrep/issues)
46+
* E-mail: `echo 'bGVhcm5ieWV4YW1wbGUubmV0QGdtYWlsLmNvbQo=' | base64 --decode`
47+
* Twitter: [https://twitter.com/learn_byexample](https://twitter.com/learn_byexample)
5248

5349
<br>
5450

@@ -72,15 +68,14 @@ Twitter: https://twitter.com/learn_byexample
7268

7369
* [GNU grep documentation](https://www.gnu.org/software/grep/manual/grep.html) — manual and examples
7470
* [ripgrep](https://github.com/BurntSushi/ripgrep) — user guide and examples
75-
* [stackoverflow](https://stackoverflow.com/) and [unix.stackexchange](https://unix.stackexchange.com/) — for getting answers to pertinent questions on `bash`, `grep` and other commands
71+
* [stackoverflow](https://stackoverflow.com/) and [unix.stackexchange](https://unix.stackexchange.com/) — for getting answers to pertinent questions on `grep` and related commands
7672
* [tex.stackexchange](https://tex.stackexchange.com/) — for help on [pandoc](https://github.com/jgm/pandoc/) and `tex` related questions
77-
* Cover image
78-
* [LibreOffice Draw](https://www.libreoffice.org/discover/draw/)
79-
* [detective](https://www.flickr.com/photos/32068925@N08/3028314931) by [olarte.ollie](https://www.flickr.com/photos/ollieolarte/) under [Creative Commons Attribution-Share Alike 2.0 Generic](https://creativecommons.org/licenses/by-sa/2.0/)
80-
* [pngquant](https://pngquant.org/) and [svgcleaner](https://github.com/RazrFalcon/svgcleaner) for optimizing images
81-
* [softwareengineering.stackexchange](https://softwareengineering.stackexchange.com/questions/39/whats-your-favourite-quote-about-programming) and [skolakoda](https://skolakoda.org/programming-quotes) for programming quotes
73+
* [/r/commandline/](https://old.reddit.com/r/commandline), [/r/linux4noobs/](https://old.reddit.com/r/linux4noobs/), [/r/linuxquestions/](https://old.reddit.com/r/linuxquestions/) and [/r/linux/](https://old.reddit.com/r/linux/) — helpful forums
74+
* [canva](https://www.canva.com/) — cover image
75+
* [oxipng](https://github.com/shssoichiro/oxipng), [pngquant](https://pngquant.org/) and [svgcleaner](https://github.com/RazrFalcon/svgcleaner) — optimizing images
8276
* [Warning](https://commons.wikimedia.org/wiki/File:Warning_icon.svg) and [Info](https://commons.wikimedia.org/wiki/File:Info_icon_002.svg) icons by [Amada44](https://commons.wikimedia.org/wiki/User:Amada44) under public domain
83-
* [Andrew Gallant](https://blog.burntsushi.net/about/) (author of `ripgrep`) and [mikeblas](https://www.reddit.com/user/mikeblas) for critical feedback
77+
* [carbon](https://carbon.now.sh/) — creating terminal screenshots with highlighted text
78+
* [Andrew Gallant](https://blog.burntsushi.net/about/) (author of `ripgrep`), [mikeblas](https://old.reddit.com/user/mikeblas) and [Pound-Hash](https://github.com/Pound-Hash) for critical feedback
8479
* [mdBook](https://github.com/rust-lang/mdBook) — for web version of the book
8580
* [mdBook-pagetoc](https://github.com/JorelAli/mdBook-pagetoc) — for adding table of contents for each chapter
8681
* [minify-html](https://github.com/wilsonzlin/minify-html) — for minifying html files
@@ -91,6 +86,7 @@ Special thanks to all my friends and online acquaintances for their help, suppor
9186

9287
# License
9388

94-
The book is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-nc-sa/4.0/)
89+
The book is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-nc-sa/4.0/).
90+
91+
The code snippets are licensed under MIT, see [LICENSE](./LICENSE) file.
9592

96-
The code snippets are licensed under MIT, see [LICENSE](./LICENSE) file

‎Version_changes.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
<br>
22

3+
### 2.0
4+
5+
* Command versions updated to **GNU grep 3.10** and **ripgrep 13.0.0**
6+
* Many more exercises added, and you can practice some of them using this [interactive TUI app](https://github.com/learnbyexample/TUI-apps/blob/main/GrepExercises)
7+
* PCRE chapter — added section for conditional grouping, corrected description and examples for `\K`, atomic grouping, etc
8+
* ripgrep chapter — options and regex section modified to present only differences compared to `GNU grep`, added details for more options such as `--field-match-separator`, improved recursive search section, etc
9+
* Long sections split into smaller ones
10+
* In general, many of the examples, exercises, solutions, descriptions and external links were updated/corrected
11+
* Updated Acknowledgements section
12+
* Code snippets related to info/warning sections will now appear as a single block
13+
* Book title changed to **CLI text processing with GNU grep and ripgrep**
14+
* New cover image
15+
* Images centered for EPUB format
16+
17+
<br>
18+
319
### 1.7
420

521
* Command versions updated to **GNU grep 3.6** and **ripgrep 12.1.1**

‎code_snippets/BRE_ERE_Regular_Expressions.sh‎

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
11
## Line Anchors
22

3-
printf 'spared no one\npar\nspar\ndare' | grep '^pa'
3+
cat anchors.txt
44

5-
printf 'spared no one\npar\nspar\ndare' | grep 'ar$'
5+
grep '^s' anchors.txt
6+
7+
grep 'rt$' anchors.txt
68

79
printf 'spared no one\npar\nspar\ndare' | grep '^par$'
810

911
printf 'spared no one\npar\nspar\ndare' | grep -x 'par'
1012

1113
## Word Anchors
1214

13-
cat word_anchors.txt
15+
cat anchors.txt
16+
17+
grep '\bpar' anchors.txt
1418

15-
grep '\bpar' word_anchors.txt
19+
grep 'par\b' anchors.txt
1620

17-
grep 'par\b' word_anchors.txt
21+
grep '\bpar\b' anchors.txt
1822

19-
grep '\bpar\b' word_anchors.txt
23+
grep -w 'par' anchors.txt
2024

21-
grep -w 'par' word_anchors.txt
25+
## Opposite Word Anchor
2226

23-
grep '\Bpar\B' word_anchors.txt
27+
grep '\Bpar\B' anchors.txt
2428

25-
grep '\Bpar' word_anchors.txt
29+
grep '\Bpar' anchors.txt
2630

27-
grep 'par\B' word_anchors.txt
31+
grep 'par\B' anchors.txt
2832

2933
## Alternation
3034

31-
printf 'I like cats\nI like parrots\nI like dogs' | grep 'cat\|dog'
35+
cat pets.txt
3236

33-
printf 'I like cats\nI like parrots\nI like dogs' | grep -E 'cat|dog'
37+
grep 'cat\|dog' pets.txt
3438

35-
printf 'I like cats\nI like parrots\nI like dogs' | grep -e 'cat' -e 'dog'
39+
grep -E 'cat|dog' pets.txt
40+
41+
grep -e 'cat' -e 'dog' pets.txt
3642

3743
printf 'CATs dog bee parrot FoX' | grep -ioE 'cat|dog|fox'
3844

39-
grep -E '^a|e\b' word_anchors.txt
45+
grep -E '^t|ar\b' anchors.txt
4046

41-
grep --color=auto -E '^|pare' word_anchors.txt
47+
grep --color -E 'sub|for|tar|' anchors.txt
4248

43-
grep --color=auto -E 'sub|put|tar|$' word_anchors.txt
49+
## Alternation precedence
4450

4551
echo 'car spared spar' | grep -oE 'are|spared'
4652

@@ -54,15 +60,15 @@ echo 'pool party 2' | grep -oP 'par|party'
5460

5561
## Grouping
5662

57-
printf 'red\nreform\nread\narrest' | grep -E 'reform|rest'
63+
printf 'red\nreform\nread\ncrest' | grep -E 'reform|rest'
5864

59-
printf 'red\nreform\nread\narrest' | grep -E 're(form|st)'
65+
printf 'red\nreform\nread\ncrest' | grep -E 're(form|st)'
6066

61-
printf 'sub par\nspare\npart time' | grep -E '\bpar\b|\bpart\b'
67+
grep -E '\bpar\b|\bpart\b' anchors.txt
6268

63-
printf 'sub par\nspare\npart time' | grep -E '\b(par|part)\b'
69+
grep -E '\b(par|part)\b' anchors.txt
6470

65-
printf 'sub par\nspare\npart time' | grep -E '\bpar(|t)\b'
71+
grep -E '\bpar(|t)\b' anchors.txt
6672

6773
## Escaping metacharacters
6874

@@ -80,21 +86,23 @@ printf '(a/b) + c\n3 + (a/b) - c' | grep -E '^\(a/b)'
8086

8187
## Matching characters like tabs
8288

83-
echo 'attempt' | grep -o 'a\tt'
84-
8589
printf 'go\tto\ngo to' | grep $'go\tto'
8690

8791
printf 'go\tto\ngo to' | grep $'go\x20to'
8892

89-
## The dot meta character
93+
echo 'sea eat car rat eel tea' | grep 's\ea'
94+
95+
## The dot metacharacter
9096

9197
echo 'tac tin cot abc:tuv excite' | grep -o 'c.t'
9298

9399
printf '42\t33\n'
94100

95101
printf '42\t33\n' | grep -o '2.3'
96102

97-
grep -xE 'c..(t|l)y' words.txt
103+
wc -l words.txt
104+
105+
grep -xE 'du.(ky|ts)' words.txt
98106

99107
## Quantifiers
100108

@@ -126,21 +134,27 @@ echo 'abc ac adc abbc xabbbcz bbb bc abbbbbc' | grep -oE 'ab{,2}c'
126134

127135
echo 'abc ac adc abbc xabbbcz bbb bc abbbbbc' | grep -oE 'ab{3}c'
128136

137+
echo 'a{5} = 10' | grep -E 'a\{5}'
138+
139+
echo 'report_{a,b}.txt' | grep -E '_{a,b}'
140+
141+
## Conditional AND
142+
129143
echo 'Error: not a valid input' | grep -o 'Error.*valid'
130144

131-
echo 'a cat and a dog' | grep -E 'cat.*dog|dog.*cat'
145+
echo 'cat and dog and parrot' | grep -oE 'cat.*dog|dog.*cat'
132146

133-
echo 'dog and cat' | grep -E 'cat.*dog|dog.*cat'
147+
echo 'dog and cat and parrot' | grep -oE 'cat.*dog|dog.*cat'
134148

135149
## Longest match wins
136150

137151
echo 'foot' | grep -oE 'f.?o'
138152

139153
echo 'car bat cod map scat dot abacus' | grep -o '.*'
140154

141-
echo 'foo123312baz' | grep -oE 'o(1|2|3)+(12baz)?'
155+
echo 'fig123312apple' | grep -oE 'g(1|2|3)+(12apple)?'
142156

143-
echo 'foo123312baz' | grep -oP 'o(1|2|3)+(12baz)?'
157+
echo 'fig123312apple' | grep -oP 'g(1|2|3)+(12apple)?'
144158

145159
echo 'car bat cod map scat dot abacus' | grep -o '.*m'
146160

@@ -160,6 +174,8 @@ echo 'do so in to no on' | grep -ow '[sot][on]'
160174

161175
grep -xE '[on]{2,}' words.txt
162176

177+
## Character class metacharacters
178+
163179
echo 'Sample123string42with777numbers' | grep -oE '[0-9]+'
164180

165181
echo 'coat Bin food tar12 best' | grep -owE '[a-z]+'
@@ -176,19 +192,25 @@ echo '0501 035 154 12 26 98234' | grep -owE '0*[1-9][0-9]{2,}'
176192

177193
echo 'Sample123string42with777numbers' | grep -oE '[^0-9]+'
178194

179-
echo 'foo=42; baz=123' | grep -o '^[^=]*'
195+
echo 'apple:123:banana:cherry' | grep -o '^[^:]*'
180196

181-
echo 'foo:123:bar:baz' | grep -oE '(:[^:]+){2}$'
197+
echo 'apple:123:banana:cherry' | grep -oE '(:[^:]+){2}$'
182198

183199
echo 'I like "mango" and "guava"' | grep -oE '"[^"]+"'
184200

185201
printf 'tryst\nfun\nglyph\npity\nwhy' | grep -xE '[^aeiou]*'
186202

187203
printf 'tryst\nfun\nglyph\npity\nwhy' | grep -v '[aeiou]'
188204

205+
## Escape sequence sets
206+
189207
printf 'load;err_msg--\nant,r2..not\n' | grep -o '\w*'
190208

191-
printf ' 1..3 \v\f foo_baz 42\tzzz \r\n1-2-3\n\n' | grep -o '\S*'
209+
echo 'sea eat car rat eel tea' | grep -o '\b\w' | paste -sd ''
210+
211+
printf ' 1..3 \v\f fig_tea 42\tzzz \r\n1-2-3\n\n' | grep -o '\S*'
212+
213+
## Named character sets
192214

193215
printf 'err_msg\nxerox\nant\nm_2\nP2\nload1\neel' | grep -x '[[:lower:]]*'
194216

@@ -198,11 +220,13 @@ printf 'err_msg\nxerox\nant\nm_2\nP2\nload1\neel' | grep -x '[[:alnum:]]*'
198220

199221
echo 'pie tie#ink-eat_42;' | grep -o '[^[:punct:]]*'
200222

223+
## Matching character class metacharacters literally
224+
201225
echo 'ab-cd gh-c 12-423' | grep -owE '[a-z-]{2,}'
202226

203-
printf 'int a[5]\nfoo\n1+1=2\n' | grep '[=]]'
227+
printf 'int a[5]\nfig\n1+1=2\n' | grep '[=]]'
204228

205-
printf 'int a[5]\nfoo\n1+1=2\n' | grep '[]=]'
229+
printf 'int a[5]\nfig\n1+1=2\n' | grep '[]=]'
206230

207231
echo 'int a[5]' | grep '[x[.y]'
208232

‎code_snippets/Context_matching.sh‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ cat context.txt
22

33
## -A
44

5-
grep -A2 'blue' context.txt
6-
75
grep -x -A2 'blue' context.txt
86

7+
grep -A2 'blue' context.txt
8+
99
## -B
1010

1111
grep -B2 'bread' context.txt
1212

1313
grep -B3 'ruby' context.txt
1414

15+
grep -A1 -B2 'sky' context.txt
16+
1517
## -C
1618

1719
grep -C1 'sky' context.txt
1820

19-
grep -A1 -B2 'sky' context.txt
20-
2121
grep -C2 'kotlin' context.txt
2222

2323
## Contiguous matches

0 commit comments

Comments
 (0)