Grep and RegEx examples
Linux Grep and Regex
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
grep regex example:
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
What is the correct way to learn Red Hat/Linux/RHCSA? This is something I am asked about frequently, so I have put together this miniseries as a starter for RHCSA Certification. The skills required for the RHCSA are quite demanding, it is a very challenging exam, but the RHCSA salary is why so many choose to train as a Red Hat Certified Engineer.
RHCSA MiniSeries
Part 1 – How to use Grep and Regular Expressions (RegEx)
Part 2 – How to manipulate files in RedHat
Part 3 – Red Hat Permissions
Part 4 – How to change the root password on Red Hat
Part 5 – How to use Red Hat as a virtual machine host
Part 6 – How to configure local storage
Part 7 – how to manage users and groups in Red Hat Linux
Part 8 – how to configure NTP Network Time Protocol in Red Hat
1 Response
[…] Part 1 – How to use Grep and Regular Expressions (RegEx) […]