Using Regular Expressions with grep


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 EXPRESSION

FUNCTION

EXAMPLE

EXPLANATION

.

Matches any character

grep b.rry

This 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/schedule

Here, 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 line

grep '^Some' sayings

With 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 line

grep 'ach$' sayings

This example finds all lines in the file sayings that end with "ach".

\

Escapes (quotes) the following characterso you can search for literal characters like * or $ that are also operators

grep '\*' sayings

grep \* 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]' specifications

Use 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 .logan  limerick 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 egrepwhen 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.





Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net