adds basic grep

This commit is contained in:
waldek 2022-05-09 17:51:12 +02:00
parent 74fac49ec2
commit 53dec51c7e
1 changed files with 46 additions and 0 deletions

View File

@ -1545,6 +1545,7 @@ This being said, you can use `grep` without regular expressions as well and this
Most distributions will come with some dictionaries installed. Most distributions will come with some dictionaries installed.
We can find the American one with the `locate` command. We can find the American one with the `locate` command.
The *third* one in line is the one we need, but have a look at the other files if you're intrigued.
``` ```
waldek@debian:~$ locate american waldek@debian:~$ locate american
@ -1581,6 +1582,51 @@ waldek@debian:~$ locate american
/var/lib/ispell/american.remove /var/lib/ispell/american.remove
``` ```
We can use `grep` to find all occurrences of the word *hippo* with the following syntax.
```
waldek@debian:~$ grep "hippop" /usr/share/dict/american-english
hippopotami
hippopotamus
hippopotamus's
hippopotamuses
waldek@debian:~$
```
We can search for multiple patterns with the `|` character **but** this charater has a special meaning in the shell so we need to escape it with a `\` **or** we can use *extended* `grep` with either `egrep` or `grep -E`.
```
waldek@debian:~$ grep "hippo\|tamu" /usr/share/dict/american-english
Metamucil
Metamucil's
hippo
hippo's
hippopotami
hippopotamus
hippopotamus's
hippopotamuses
hippos
whippoorwill
whippoorwill's
whippoorwills
waldek@debian:~$ grep -E "hippo|tamu" /usr/share/dict/american-english
Metamucil
Metamucil's
hippo
hippo's
hippopotami
hippopotamus
hippopotamus's
hippopotamuses
hippos
whippoorwill
whippoorwill's
whippoorwills
waldek@debian:~$
```
Adding the `--color` argument to `grep will make the matched patterns jump out with a color, depending on the color scheme of your terminal.
## Wildcards and regular expressions ## Wildcards and regular expressions
# Exercise # Exercise