Using Regular Expressions with grep

In addition to using grep to search for simple text strings, you can also use grep to search for regular expressions. Regular expressions are kind of like fancy wildcards, where you use a symbol to represent a character, number, or other symbol. With regular expressions, you can search for different parts of files, such as the end of a line or a text string next to another specified text string. Table 6.1 lists some of the more common regular expressions.

Table 6.1. Regular Expressions, Examples, and Explanations
REGULAR EXPRESSIONFUNCTIONEXAMPLEEXPLANATION
.Matches any charactergrep b.rryThis finds all instances of “berry” or “barry.”
*Matches zero or more instances of the preceding item, so a*b would find “b” as well as “ab” and “aaab,” but not “acb”grep ‘s*day‘ /home/ejr/scheduleHere, the * matches zero or more of the items that immediately precede the *, in this case the letter ‘s’.
^Matches only instances of the string at the beginning of a linegrep ‘^Some’ sayingsWith the ^, you specify that the search string must appear at the beginning of a line. The example would find a line beginning with “Some” but not one beginning with “Read Some.”
$Matches only instances of the string at the end of a linegrep ‘ach$’ sayingsThis example finds all lines in the file sayings that end with “ach”.
Escapes (quotes) the following character—so you can search for literal characters like * or $ that are also operatorsgrep ‘*’ sayingsgrep * saying searches for all instances of “*” in the sayings file. The tells grep to interpret the * literally, as an asterisk character, rather than as a wildcard.
[ ]Matches any member of the set, like [a-z], [0-6], or [321] (three or two or one)grep ‘number[0-9]’ specificationsUse square brackets ([ ]) to enclose a set of options. Here, number[0-9] would match all instances of number1, number2, number3, and so forth in the file called specifications.

Code Listing 6.9. Use grep with regular expressions to create fancy wildcard commands.
[ejr@hobbes manipulate]$ grep .loganlimerick
Worked hard all day on a slogan,
You see, the slogan's still brogan.
[ejr@hobbes manipulate]$

To use regular expressions with grep:

  • grep .logan limerick

    Type grep followed by the regular expression and the filename. Here, we’ve used the regular expression .logan to find all instances of “logan” that are preceded by a single character (Code Listing 6.9). Note that this usage of a . to match a single character closely resembles the ? wildcard used with ls.

    You could also use multiple periods for specific numbers of characters. For example, to find “Dogbert” and “Dilbert,” you might use grep D..bert plagiarized. sayings.

    In some cases, you may need to structure the search string slightly differently, depending on the expression you’re using and the information you’re looking for. Check out the additional examples in Table 6.1 for more information.

✓ Tips

  • “Regular expression” is often abbreviated as “regexp” in Unix documentation and Internet discussions.

  • The command egrep is closely related to grep, adding a little more flexibility for extended regular expressions, but it fundamentally works the same. On many systems the grep command is really egrep—when you type in either one, you’re really running egrep.

  • If you’re searching for whole words through large files, use fgrep for faster searching. It uses the same general syntax as grep, but searches only for whole words (not regular expressions) so goes much faster.

  • See Chapter 1 for details about wildcards.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.73