githubEdit

rectangle-terminalgrep

When investigating bugs, crashes, or unexpected behavior, analyzing logs becomes crucial. The grep command is one of the most powerful and essential tools in Linux for this purpose.


Basic Syntax

grep [options] pattern [file...]

Option Examples

Option
Example
Description

-i

Ignore case sensitivity.

-w

Search for the full word only.

-A

Show 3 lines after the match.

-B

Show 4 lines before the match.

-C

Show 5 lines around the match.

-r

Recursive search in subdirectories.

-v

Show lines that do not match.

-e

Use regex (basic regular expressions).

-E

Use extended regex.

-c

Count the number of matches.

-l

Print filenames of matches.

-o

Only show the matching part.

-n

Show line numbers of matches.


grep Regular Expressions

Wildcards

  • . Any single character

  • ? Optional, occurs once

  • * Optional, occurs multiple times

  • + Required, occurs multiple times

Quantifiers

  • {n} Exactly n times

  • {n,} At least n times

  • {,m} Up to m times

  • {n,m} Between n and m times

POSIX Character Classes

  • [:alpha:] Any letter (a-zA-Z)

  • [:digit:] Any digit (0-9)

  • [:alnum:] Any letter or digit

  • [:space:] Any whitespace

Character Ranges

  • [A-Za-z] Any letter

  • [0-9] Any digit

  • [0-9A-Za-z] Any letter or digit

Positions

  • ^ Beginning of line

  • $ End of line

  • ^$ Empty line

  • \< Start of word

  • \> End of word

Most Useful grep Commands

1. Find a Specific Keyword

Find lines containing "error" in application.log.


Match "warning", "Warning", "WARNING", etc.


Search inside all files under /var/log/ directory.


4. Show Line Numbers

Display matching lines with line numbers.


5. Show Lines Before/After/Context

  • Show 5 lines after match:

  • Show 5 lines before match:

  • Show 5 lines before and after match:

Very useful to understand surrounding context of an error.


Match any of the words: fatal, panic, or critical.


7. Invert Match

Show lines excluding "INFO". Helps to filter out noise.


8. Count Occurrences

Display how many times "timeout" appears.


9. Search Whole Words

Match "start" exactly β€” not "restart" or "started".


10. Live Log Monitoring

Monitor a log in real-time and only show new lines containing "error".


Save Results to a File

Redirect grep results into a file for later review.


Piping with grep

When analyzing logs, you often deal with large outputs or want to chain commands together. That's where piping (|) becomes extremely powerful with grep.

Piping allows you to take the output of one command and pass it directly as input to grep, without needing intermediate files.


Basic Syntax

| sends the output of the left-hand command to the input of the right-hand command.


Common Piping Examples

1. Filter dmesg Logs for USB Events

Search for "usb" events in kernel logs.


2. Find Running Processes

List all running processes and filter those related to "python".


3. Combine with tail for Real-Time Monitoring

Monitor a growing log file and only show lines containing "error".


4. Chain Multiple Pipes

You can chain pipes for even more advanced filtering:

Find "timeout" entries but exclude lines mentioning "retry".


5. Use with ls to Search for Files

List only nginx-related files inside /var/log.


Quick Tip: Grep Output Colors

You can enable colored output when using grep with pipes:

Highlight matching text for better visibility.

Resources for Practicing


grep Cheatsheet


Next: You can level up further by learning awk and sed for advanced log analysis!

Last updated