0% found this document useful (0 votes)
2 views6 pages

2.GrepRegEx

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

2.GrepRegEx

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Red Hat Certified

System
Administrator

Using grep and


Regular Expressions
What is grep?
grep is a program designed for
finding matching patterns.
It can be used by itself:
grep Error /var/log/messages

Or as part of a pipe:
find . –name *.txt | grep taxes

Course Title
grep Common Flags
grep
-i: Makes the search case
insensitive

-r: Makes the search recursive


through a directory structure

-v: Makes the search find all


instances where there is not a
pattern match

-w: Makes the search match on a


word rather than any pattern
match

Course Title
grep Regular Expressions Pt. 1
^ Match expression at the start
of a line, as in ^A

$ Match expression at the end


of a line, as in A$.

\ Turn off the special meaning


of the next character, as in \^

[ ] Match any one of the


enclosed characters, as in
[aeiou], and use a hyphen
for a range, as in [0-9]

[^ ] Match any one character


except those enclosed in [ ],
as in [^0-9]

Course Title
grep Regular Expressions Pt. 2

. Match a single character of


any value, except end of line

* Match zero or more of the


preceding character or
expression

\{x,y\} Match x to y occurrences


of the preceding

\{x\} Match exactly x occurrences


of the preceding

\{x,\} Match x or more


occurrences of the preceding

Course Title
grep Regular Expressions
Search files for lines with the word
linux:
grep linux files
With linux at the start of a line:
grep '^linux' files
With linux at the end of a line:
grep 'linux$' files
Show lines containing only linux:
grep '^linux$' files
Lines starting with '^s', \ escapes the ^:
grep '\^s' files
Search for either Linux or linux:
grep '[Ll]inux' files
Search for BOB, Bob, BOb or BoB:
grep 'B[oO][bB]' files
Search for blank lines:
grep '^$' files
Search for pairs of numeric digits:
grep '[0-9][0-9]' file

Course Title

You might also like