CLH-011

ADVANCED GREP & REGEX

< Script House
operator@shadow:~
[SYSTEM] Pattern matching module loaded
[MISSION] Hunt for specific patterns in log files using regex
> Mission: Master grep flags and regular expressions
Type help for available commands.
operator@shadow:~$

CURRENT OBJECTIVE

1
2
3
4
5

HUNT: Case-Insensitive Search

Find all lines containing "error" (case-insensitive) in system.log using grep -i.

$ grep -i "error" logs/system.log

PATTERN HUNTER'S GUIDE

The Mission

Regular expressions are the language of pattern matching. Master grep and regex to hunt through massive log files and extract exactly what you need.

Grep Flags

-i
Case insensitive search
-v
Invert match (exclude pattern)
-c
Count matching lines
-n
Show line numbers
-r
Recursive search in directories
-E
Extended regex (same as egrep)

Regex Basics

PatternMatches
.Any single character
*Zero or more of previous
+One or more (extended regex)
^Start of line
$End of line
[abc]Any character in set
[0-9]Any digit
\dAny digit (extended)

IP Address Pattern

grep -E "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"
Match IP addresses
PRO TIP:

Use grep -E or egrep for extended regex with +, ?, |, and () without escaping.

REGEX GOTCHA:

In basic grep, special chars like + and | must be escaped: \+ and \|