Skip to content
This repository was archived by the owner on Jun 5, 2024. It is now read-only.

Commit 3151e05

Browse files
some more examples, clarifications, link to grep book
1 parent a9cd336 commit 3151e05

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

gnu_grep.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747

4848
<br>
4949

50+
This chapter has also been [converted to a book](https://github.com/learnbyexample/learn_gnugrep_ripgrep) with additional examples, exercises and covers popular alternative `ripgrep`
51+
52+
<br>
53+
5054
```bash
5155
$ grep -V | head -1
5256
grep (GNU grep) 2.25
@@ -245,6 +249,8 @@ sugar
245249
246250
* Word search using `-w` option
247251
* word constitutes of alphabets, numbers and underscore character
252+
* This will ensure that given patterns are not surrounded by other word characters
253+
* this is slightly different than using word boundaries in regular expressions
248254
* For example, this helps to distinguish `par` from `spar`, `part`, etc
249255
250256
```bash
@@ -285,6 +291,7 @@ car
285291
* `never` explicitly specify no highlighting
286292
287293
```bash
294+
$ # can also use grep --color 'blue' as auto is default
288295
$ grep --color=auto 'blue' poem.txt
289296
Violets are blue,
290297
```
@@ -302,6 +309,13 @@ Violets are blue,
302309
$ grep --color=always 'blue' poem.txt > saved_output.txt
303310
$ cat -v saved_output.txt
304311
Violets are ^[[01;31m^[[Kblue^[[m^[[K,
312+
313+
$ # some commands like 'less' are capable of using the color information
314+
$ grep --color=always 'are' poem.txt | less -R
315+
$ # highlight multiple matching patterns
316+
$ grep --color=always 'are' poem.txt | grep --color 'd'
317+
Roses are red,
318+
And so are you.
305319
```
306320
307321
<br>
@@ -347,6 +361,7 @@ Sugar is sweet,
347361
```
348362
349363
* If there are multiple non-adjacent matching segments, by default `grep` adds a line `--` to separate them
364+
* non-adjacent here implies that segments are separated by at least one line
350365
351366
```bash
352367
$ seq 29 | grep -A1 '3'
@@ -372,7 +387,7 @@ $ seq 29 | grep --no-group-separator -A1 '3'
372387
24
373388
```
374389
375-
* Use `--group-separator` to specify an alternate separator
390+
* Use `--group-separator` to customize the separator
376391
377392
```bash
378393
$ seq 29 | grep --group-separator='*****' -A1 '3'
@@ -390,7 +405,6 @@ $ seq 29 | grep --group-separator='*****' -A1 '3'
390405
391406
## <a name="recursive-search"></a>Recursive search
392407
393-
394408
First let's create some more test files
395409
396410
```bash
@@ -547,6 +561,7 @@ $ find -type f -not -name '*.txt' -exec grep -in 'you' {} +
547561
548562
```bash
549563
$ # prompt at end of line not shown for simplicity
564+
$ # ^@ here indicates the NUL character
550565
$ grep -rlZ 'you' | cat -A
551566
poem.txt^@test_files/hidden_files/.fav_color.info^@
552567

0 commit comments

Comments
 (0)