Text Searching & Regex with Grep
Grep (Global Regular Expression Print) is the ultimate tool for log analysis, debugging errors, and extracting critical strings from large datasets.
Lesson Objectives
4 Steps1Step 1: Search for Error Messages in Syslog
Run `grep -i "error" /var/log/syslog` to perform a case-insensitive search for system errors.
2Step 2: Show Line Numbers with Matches
Run `grep -n -i "accepted" /var/log/auth.log` to find successful authentication events with exact line numbers.
3Step 3: Count Total Matching Lines
Run `grep -c "session" /var/log/auth.log` to return the total number of matched lines.
4Step 4: Invert Match to Exclude Comments
Run `grep -v "^#" /etc/hosts` to display only active non-comment configuration lines.
💡 Pro Tip:
Combine `-i` (case-insensitive) and `-n` (line numbers) when debugging production log files for rapid troubleshooting.
bash — terminal
AST Sandbox (Free)