Chapter 5. Step 5: Search Me
In the preceding chapter, you learned to search for files by their attributes, such as name, last modified time, and the like. In this chapter, I show how to search inside a file, perhaps to find a specific error message.
Getting a grep
The grep
command (whose name comes from globally search a regular
expression and print) searches within files. It uses regular
expressions (regex) to match patterns inside the files. It can be used
to search within binary files, but is most useful for finding things
inside text files. There are lots of uses for this command in our crisis
scenario, such as searching for certain error messages within log files,
or finding every mention of a certain resource inside the source files
for an entire website.
There is an old joke by Jamie Zawinski:
Some people, when confronted with a problem, think, “I know, I’ll use regular expressions.” Now they have two problems.
Some regular expressions are simple—for example, *
, which you should
recognize as a valid wildcard in Windows Command Prompt. Others can be
mind-blowingly complex. For example:
^\(*\d{3}\)*( |-)*\d{3}( |-)*\d{4}$
This regular expression is an (incomplete) approach to matching US phone numbers.
Because regexes are so inscrutable, sometimes I write a regex in a program or a script, come back to it six months later, and have no idea what it is doing. (Now I have two problems.) In this chapter, you’re just going to look at a few simple examples.
Here are some samples ...
Get Ten Steps to Linux Survival now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.