Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and clarify grep regex usage in 05.Rmd #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions book/2e/05.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,22 @@ For example, if you only wanted to print the headings that start with *The*:
< alice.txt grep -E '^CHAPTER (.*)\. The'
```

Note that you have to specify the `-E` option in order to enable regular expressions.
Otherwise, `grep` interprets the pattern as a literal string which most likely results in no matches at all:
`-E` is for enabling the ERE (extended regular expression) standard. By default, `grep` uses the BRE (basic regular expression). ERE and BRE support different syntaxes, and each could be the more appropriate choice depending on the situation.

To achieve the same result with BREs, run

```{console}
< alice.txt grep -G '^CHAPTER \(.*\)\. The'
```

This command employs the `-G` flag, indicating the usage of BRE. It is equivalent to:

```{console}
< alice.txt grep '^CHAPTER (.*)\. The'
< alice.txt grep '^CHAPTER \(.*\)\. The'
```

which defaults to using BRE.

With the `-v` option you invert the matches, so that `grep` prints the lines which *don't* match the pattern.
The regular expression below matches lines that contain white space, only.
So with the inverse, and using `wc -l`, you can count the number of non-empty lines:
Expand Down