Skip to content

Commit 15648b8

Browse files
updates for version 1.7
1 parent 87acd5c commit 15648b8

13 files changed

Lines changed: 328 additions & 238 deletions

‎README.md‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ You can purchase the book using these links:
2121
* https://gumroad.com/l/gnugrep_ripgrep
2222
* https://leanpub.com/gnugrep_ripgrep
2323
* You can also get the book as part of **Magical one-liners** bundle from https://gumroad.com/l/oneliners or https://leanpub.com/b/oneliners
24-
* You can also get the book as part of **Awesome Regex** bundle from https://leanpub.com/b/regex or https://gumroad.com/l/regex
24+
* You can also get the book as part of **Awesome Regex** bundle from https://gumroad.com/l/regex or https://leanpub.com/b/regex
2525
* See https://learnbyexample.github.io/books/ for list of other books
2626

27-
For a preview of the book, see [sample chapters](https://github.com/learnbyexample/learn_gnugrep_ripgrep/blob/master/sample_chapters/grep_sample_chapters.pdf)
27+
For a preview of the book, see [sample chapters](https://github.com/learnbyexample/learn_gnugrep_ripgrep/blob/master/sample_chapters/gnu_grep_sample.pdf)
2828

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

@@ -34,11 +34,11 @@ For web version of the book, visit https://learnbyexample.github.io/learn_gnugre
3434

3535
# Feedback
3636

37-
Please open an issue if you spot any typo/errors.
37+
[Open an issue](https://github.com/learnbyexample/learn_gnugrep_ripgrep/issues) if you spot any typo/errors.
3838

39-
I'd also highly appreciate your feedback about the book.
39+
:warning: :warning: Please DO NOT submit pull requests. Main reason being any modification requires changes in multiple places.
4040

41-
Goodreads: https://www.goodreads.com/book/show/47406700-gnu-grep-and-ripgrep
41+
I'd also highly appreciate your feedback about the book.
4242

4343
Twitter: https://twitter.com/learn_byexample
4444

@@ -65,10 +65,11 @@ Twitter: https://twitter.com/learn_byexample
6565
* [GNU grep documentation](https://www.gnu.org/software/grep/manual/grep.html) — manual and examples
6666
* [ripgrep](https://github.com/BurntSushi/ripgrep) — user guide and examples
6767
* [stackoverflow](https://stackoverflow.com/) and [unix.stackexchange](https://unix.stackexchange.com/) — for getting answers to pertinent questions on `bash`, `grep` and other commands
68-
* [tex.stackexchange](https://tex.stackexchange.com/) — for help on `pandoc` and `tex` related questions
68+
* [tex.stackexchange](https://tex.stackexchange.com/) — for help on [pandoc](https://github.com/jgm/pandoc/) and `tex` related questions
6969
* Cover image
7070
* [LibreOffice Draw](https://www.libreoffice.org/discover/draw/)
7171
* [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/)
72+
* [pngquant](https://pngquant.org/) and [svgcleaner](https://github.com/RazrFalcon/svgcleaner) for optimizing images
7273
* [softwareengineering.stackexchange](https://softwareengineering.stackexchange.com/questions/39/whats-your-favourite-quote-about-programming) and [skolakoda](https://skolakoda.org/programming-quotes) for programming quotes
7374
* [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
7475
* [Andrew Gallant](https://blog.burntsushi.net/about/) (author of `ripgrep`) and [mikeblas](https://www.reddit.com/user/mikeblas) for critical feedback
@@ -85,3 +86,4 @@ Special thanks to all my friends and online acquaintances for their help, suppor
8586
The book is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-nc-sa/4.0/)
8687

8788
The code snippets are licensed under MIT, see [LICENSE](./LICENSE) file
89+

‎Version_changes.md‎

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

3+
### 1.7
4+
5+
* Command versions updated to **GNU grep 3.6** and **ripgrep 12.1.1**
6+
* Added examples for using `--include` and `--exclude` when recursive option is not active
7+
* Added examples which require `**` in the `glob` pattern with `rg -g`
8+
* Updated documentation snippets as per new `man` and `info` pages
9+
* Updated timing data for speed comparison examples
10+
* Corrected various typos, improved descriptions/comments/examples/exercises/etc
11+
12+
<br>
13+
314
### 1.6
415

516
* Clarified BRE vs ERE difference for line anchor escaping

‎code_snippets/Frequently_used_options.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ grep -f search_strings.txt -e 'twice' programming_quotes.txt
7070

7171
grep 'in' programming_quotes.txt | grep 'not'
7272

73-
## Filename instead of matching lines
73+
## Get filename instead of matching lines
7474

7575
grep -l 'are' programming_quotes.txt search_strings.txt
7676

‎code_snippets/Gotchas_and_Tricks.sh‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,11 @@ time LC_ALL=C grep -xE '([a-z]..)\1' words.txt > f2
6262

6363
rm f[12]
6464

65-
/bin/grep --version | head -n1
66-
67-
time /bin/grep -wE '([a-d][r-z]){3}' words.txt > f1
68-
69-
grep --version | head -n1
70-
71-
time grep -wE '([a-d][r-z]){3}' words.txt > f2
72-
73-
rm f[12]
74-
7565
## Speed benefits with PCRE
7666

7767
time LC_ALL=C grep -xE '([a-z]..)\1' words.txt > f1
7868

79-
time LC_ALL=C grep -xP '([a-z]..)\1' words.txt > f2
69+
time grep -xP '([a-z]..)\1' words.txt > f2
8070

8171
rm f[12]
8272

@@ -94,7 +84,7 @@ time grep -rl 'include' . > ../f1
9484

9585
time find -type f -print0 | xargs -0 grep -l 'include' > ../f2
9686

97-
time find -type f -print0 | xargs -0 -P2 grep -l 'include' > ../f3
87+
time find -type f -print0 | xargs -0 -P0 grep -l 'include' > ../f3
9888

9989
diff -sq <(sort ../f1) <(sort ../f2)
10090

‎code_snippets/Introduction.sh‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## Installation
22

3-
wget https://ftp.gnu.org/gnu/grep/grep-3.4.tar.xz
3+
wget https://ftp.gnu.org/gnu/grep/grep-3.6.tar.xz
44

5-
tar -Jxf grep-3.4.tar.xz
5+
tar -Jxf grep-3.6.tar.xz
66

7-
cd grep-3.4/
7+
cd grep-3.6/
88

99
./configure
1010

‎code_snippets/Perl_Compatible_Regular_Expressions.sh‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ echo '5ba\babc2' | grep -oP '[a\b]*'
1616

1717
echo '5ba\babc2' | grep -oP '[a\\b]*'
1818

19-
echo 'w=y\x+9*3' | grep -oE '[\w=]+'
19+
echo 'w=y\x+9' | grep -oE '[\w=]+'
2020

21-
echo 'w=y\x+9*3' | grep -oP '[\w=]+'
21+
echo 'w=y\x+9' | grep -oP '[\w=]+'
2222

2323
echo '123456789abc42az' | grep -E '(.)(.)(.)(.)(.)(.)(.)(.)(.)(.).*\10'
2424

@@ -40,11 +40,11 @@ cat five_words.txt
4040

4141
printf 'sub\nbit' | grep -f- five_words.txt
4242

43-
grep -e 'sub' -e 'bit' five_words.txt
43+
grep -e 'sub' -e 'bit' five_words.txt
4444

4545
printf 'sub\nbit' | grep -P -f- five_words.txt
4646

47-
grep -P -e 'sub' -e 'bit' five_words.txt
47+
grep -P -e 'sub' -e 'bit' five_words.txt
4848

4949
## String anchors
5050

@@ -146,9 +146,9 @@ echo 'cat scatter cater scat' | grep -oP '(?<=(cat.*?){2})cat[a-z]*'
146146

147147
echo 'foo=42, bar=314' | grep -oP '=\K\d+'
148148

149-
echo 'cat scatter cater scat' | grep -oP '(cat.*?){2}\Kcat[a-z]*'
149+
echo 'cat scatter cater scat' | grep -oP '^(.*?cat.*?){2}\Kcat[a-z]*'
150150

151-
echo 'or42 car3 pare7 care5' | grep -oP '\b[a-z]{1,3}\K\d+'
151+
echo 'or42 pare7 or3 cared5' | grep -oP '\b[a-z]{1,3}\K\d+'
152152

153153
echo 'fox,cat,dog,parrot' | grep -qP '^((?!cat).)*dog' || echo 'No match'
154154

@@ -208,7 +208,7 @@ printf '@A-.\tcar' | grep -oP '\G\S'
208208

209209
## Skipping matches
210210

211-
echo 'car bat cod map' | grep -oP '(bat|map)(*SKIP)(*F)|\w+'
211+
echo 'car bat cod map' | grep -oP '\b(bat|map)\b(*SKIP)(*F)|\w+'
212212

213213
echo 'I like2 "mango" and "guava"' | grep -oP '"[^"]+"(*SKIP)(*F)|\w+'
214214

‎code_snippets/Recursive_search.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ grep -Rl --include='*.txt' --include='*.py' 'in'
4646

4747
printf '*en\n*.txt' | grep -Rl --exclude-from=- 'in'
4848

49+
grep -Rl --include='*on*' --exclude='*.txt' 'in'
50+
51+
grep -Rl --exclude='*.txt' --include='*on*' 'in'
52+
53+
grep -l 'a' scripts/*
54+
55+
grep -l --exclude='*.sh' 'a' scripts/*
56+
57+
grep -l --include='*.sh' 'a' scripts/*
58+
4959
## Extended globs
5060

5161
shopt -s extglob globstar

‎code_snippets/ripgrep.sh‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
link='https://github.com/BurntSushi/ripgrep/releases/'
44

5-
link="$link"'download/12.1.0/ripgrep_12.1.0_amd64.deb'
5+
link="$link"'download/12.1.1/ripgrep_12.1.1_amd64.deb'
66

77
wget "$link"
88

9-
sudo gdebi ripgrep_12.1.0_amd64.deb
9+
sudo gdebi ripgrep_12.1.1_amd64.deb
1010

1111
rg --version
1212

@@ -74,7 +74,7 @@ rg -f search_strings.txt -e 'twice' programming_quotes.txt
7474

7575
rg 'in' programming_quotes.txt | rg 'not'
7676

77-
## Filename instead of matching lines
77+
## Get filename instead of matching lines
7878

7979
rg -l 'are' programming_quotes.txt search_strings.txt
8080

@@ -414,7 +414,9 @@ echo 'foo=42, bar=314, baz:512' | rg -o '=(\d+)' -r '$1'
414414

415415
echo '42 foo-5, baz3; x-83, y-20: f12' | rg -o '\-(\d+)[:;]' -r '$1'
416416

417-
echo 'cat scatter cater scat' | rg -o '(?:cat.*?){2}(cat[a-z]*)' -r '$1'
417+
s='scatter cat cater scat concatenate abdicate'
418+
419+
echo "$s" | rg -o '^(?:.*?cat.*?){2}(cat[a-z]*)' -r '$1'
418420

419421
echo 'a b a' | rg 'a' -r '$${a}'
420422

@@ -476,7 +478,9 @@ rg -P '(?=.*a)(?=.*e)(?=.*i)(?=.*o).*u' five_words.txt
476478

477479
echo 'hey food! foo42 foot5 foofoo' | rg -P 'foo(?!\d)' -r 'X'
478480

479-
echo 'cat scatter cater scat' | rg -oP '(cat.*?){2}\Kcat[a-z]*'
481+
s='scatter cat cater scat concatenate abdicate'
482+
483+
echo "$s" | rg -oP '^(.*?cat.*?){2}\Kcat[a-z]*'
480484

481485
echo 'fox,cat,dog,parrot' | rg -qP 'at((?!go).)*par' && echo 'Match'
482486

@@ -486,9 +490,9 @@ expr='(a^b)'
486490

487491
echo 'f*(2-a/b) - 3*(a^b)-42' | rg -oP '\S*\Q'"$expr"'\E\S*'
488492

489-
echo 'car bat cod map' | rg -o '(bat|map)(*SKIP)(*F)|\w+'
493+
echo 'car bat cod map' | rg -o '\b(bat|map)\b(*SKIP)(*F)|\w+'
490494

491-
echo 'car bat cod map' | rg -o --engine=auto '(bat|map)(*SKIP)(*F)|\w+'
495+
echo 'car bat cod map' | rg -o --engine=auto '\b(bat|map)\b(*SKIP)(*F)|\w+'
492496

493497
## Recursive options
494498

@@ -536,6 +540,8 @@ rg 'red'
536540

537541
rg -Ll 'red'
538542

543+
rg --type-list | rg 'markdown'
544+
539545
rg -t=py -t=sh --files
540546

541547
rg -t=txt --files
@@ -550,6 +556,20 @@ rg -g='!*.py' --files
550556

551557
rg -g='!scripts' --files
552558

559+
mkdir double_star && cd $_
560+
561+
mkdir -p one/two/{x,y,z}/four
562+
563+
touch one/1.txt one/two/y/why.txt one/two/x/ex.txt one/two/y/four/4.txt
564+
565+
rg --files
566+
567+
rg -g='one/two/**' --files
568+
569+
rg -g='**/y/**' --files
570+
571+
cd .. && rm -r double_star
572+
553573
## Speed comparison
554574

555575
time grep -rw 'user' > ../f1

‎exercises/Exercise_solutions.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
# Frequently used options
66

7-
Create `exercises` directory and within it, create another directory for this chapter, say `freq_options` or `chapter_2`. Input is a file downloaded from internet — https://www.gutenberg.org/files/345/345.txt saved as `dracula.txt`.
7+
First create `exercises` directory and then within it, create another directory for this chapter, say `freq_options` or `chapter_2`. Input is a file downloaded from internet — https://www.gutenberg.org/files/345/old/345.txt saved as `dracula.txt`.
88

99
**a)** Display all lines containing `ablaze`
1010

1111
```bash
1212
$ mkdir -p exercises/freq_options && cd $_
13-
$ wget https://www.gutenberg.org/files/345/345.txt -O dracula.txt
13+
$ wget https://www.gutenberg.org/files/345/old/345.txt -O dracula.txt
1414

1515
$ grep 'ablaze' dracula.txt
1616
the room, his face all ablaze with excitement. He rushed up to me and
@@ -333,7 +333,7 @@ $ printf 'hi there\0good day\n' | grep -a 'good'
333333
hi theregood day
334334
```
335335
336-
**b)** Read about `--line-buffered` from the manual (also [this link](https://stackoverflow.com/questions/7161821/how-to-grep-a-continuous-stream)) and see it in action with code below:
336+
**b)** Read about `--line-buffered` from the manual (also [this link](https://stackoverflow.com/q/7161821/4082052)) and see it in action with code below:
337337
338338
```bash
339339
$ for i in {1..5}; do seq 12; sleep 1; done | grep '[1-489]' | grep -v '0'

‎exercises/Exercises.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# Exercises
22

3+
>![info](../images/info.svg) For solutions, see [Exercise_solutions.md](https://github.com/learnbyexample/learn_gnugrep_ripgrep/blob/master/exercises/Exercise_solutions.md).
4+
35
<br>
46

57
# Frequently used options
68

7-
Create `exercises` directory and within it, create another directory for this chapter, say `freq_options` or `chapter_2`. Input is a file downloaded from internet — https://www.gutenberg.org/files/345/345.txt saved as `dracula.txt`. To solve the exercises, modify the partial command shown just before the expected output.
9+
First create `exercises` directory and then within it, create another directory for this chapter, say `freq_options` or `chapter_2`. Input is a file downloaded from internet — https://www.gutenberg.org/files/345/old/345.txt saved as `dracula.txt`. To solve the exercises, modify the partial command shown just before the expected output.
810

911
**a)** Display all lines containing `ablaze`
1012

1113
```bash
1214
$ mkdir -p exercises/freq_options && cd $_
13-
$ wget https://www.gutenberg.org/files/345/345.txt -O dracula.txt
15+
$ wget https://www.gutenberg.org/files/345/old/345.txt -O dracula.txt
1416

1517
$ grep ##### add your solution here
1618
the room, his face all ablaze with excitement. He rushed up to me and
@@ -319,7 +321,7 @@ $ printf 'hi there\0good day\n' | grep ##### add your solution here
319321
hi theregood day
320322
```
321323
322-
**b)** Read about `--line-buffered` from the manual (also [this link](https://stackoverflow.com/questions/7161821/how-to-grep-a-continuous-stream)) and see it in action with code below:
324+
**b)** Read about `--line-buffered` from the manual (also [this link](https://stackoverflow.com/q/7161821/4082052)) and see it in action with code below:
323325
324326
```bash
325327
$ for i in {1..5}; do seq 12; sleep 1; done | grep '[1-489]' | grep -v '0'

0 commit comments

Comments
 (0)