Grep and RegEx

This is the first of a mini series focusing on some of the key elements of the Red Hat RHCSA exam. The grep command is a command-line utility for searching plain-text data sets for lines that match a regular expression. To use the command, simply type grep. grep can be piped from and to many other commands/operators to fine tune the search.

Command Format: cat file.txt | grep word

example: cat ansible.cfg | grep plugins

Grep common flags

here are some of the most common grep commands

-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

Regular expression

A “regular expression” is a text string that describes a particular search pattern. (Examples further down)

here are some of the most common regular expression operator:

^ 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]

. 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

Grep regular expression examples

Search files for lines with the word linux:

Command Format: grep linux files

With linux at the start of a line:

Command Format: grep '^linux' files
example: sudo grep '^May' /var/log/yum.log

With linux at the end of a line:

Command Format:  grep 'linux$' files
example: sudo grep 'root$' /var/log/secure

Show lines containing only linux:

Command Format:  grep '^linux$' files
To demonstrate this RegEx I created a testfile containing:
turbogeek
turbogeek.co.uk
www.turbogeek.co.uk
https://www.turbogeek.co.uk
Example: grep '^turbogeek$' testfile

Lines starting with ‘^s’, \ escapes the ^:

Command Format:  grep '\^s' files
example: grep '\turbogeek^s' testfile
Note: Output = null as grep ignores

Search for either case sensitive eg. Linux or linux:

Command Format:  grep '[Ll]inux' files
To demonstrate this I updated the testfile
turbogeek
Turbogeek
turbogeek.co.uk
Turbogeek.co.uk
www.turbogeek.co.uk
https://www.turbogeek.co.uk
grep '[T]urbogeek' testfile
grep '[t]urbogeek' testfile

Search for BOB, Bob, BOb or BoB:

Command Format:  grep 'B[oO][bB]' files

Search for blank lines:

Command Format:  grep '^$' files

Search for pairs of numeric digits:

Command Format:  grep '[0-9][0-9]' file

search for uat, replace with sit, globally

Command Format:  :%s/uat/sit/g