12.7. Bonus section for Unix / Linux users¶
Support for searching files using regular expressions was built into the Unix operating system since the 1960s and it is available in nearly all programming languages in one form or another.
As a matter of fact, there is a command-line program built into Unix
called grep (Generalized Regular Expression Parser)
that does pretty much the same as the search()
examples in
this chapter. So if you have a Macintosh or Linux system, you can try
the following commands in your command-line window.
$ grep '^From:' mbox-short.txt
From: [email protected]
From: [email protected]
From: [email protected]
From: [email protected]
This tells grep
to show you lines that start with the
string “From:” in the file mbox-short.txt. If you
experiment with the grep
command a bit and read the
documentation for grep
, you will find some subtle
differences between the regular expression support in Python and the
regular expression support in grep
. As an example,
grep
does not support the non-blank character
\S
so you will need to use the slightly more complex set
notation [^ ]
, which simply means match a character that is anything
other than a space.
- $ grep '\$[0-9.]+' mbox-short.txt
- Correct! This is the correct way to use grep to find prices in a file.
- $ grep '\$[0-9]+'
- Make sure to include the file!
- $ grep '\$[/d.]+' mbox-short.txt
- Correct! This uses the /d regex character to find prices in mbox-short.txt.
- $ grep '$[0-9.]+' mbox-short.txt
- You're missing a character.
11-9-1: Which option(s) properly uses the grep command-line to search for prices? Select all that apply.