grep Command

The grep command (Global Regular Expression Print) searches for text patterns in files using regular expressions. It's one of the most powerful and frequently used commands for text processing, log analysis, and data extraction in Linux and Unix systems.

Syntax

grep [options] pattern [file...]

Description

The grep command searches through files line by line, looking for lines that match a specified pattern. When a match is found, grep prints the entire line to standard output. It supports both simple text matching and complex regular expressions.

Key features:

  • Pattern matching with regular expressions
  • Case-sensitive and case-insensitive searching
  • Recursive directory searching
  • Line number and filename display
  • Inverse matching (show non-matching lines)

Common Options

Option Description Example
-i Case-insensitive search grep -i "error" log.txt
-r, -R Recursive search in directories grep -r "TODO" src/
-n Show line numbers grep -n "function" code.c
-v Invert match (show non-matching lines) grep -v "comment" file.txt
-c Count matching lines grep -c "error" log.txt
-l Show only filenames with matches grep -l "main" *.c
-E Extended regular expressions grep -E "(error|warning)" log.txt

Common Regular Expression Patterns

  • ^pattern - Match at beginning of line
  • pattern$ - Match at end of line
  • . - Match any single character
  • * - Match zero or more of preceding character
  • [abc] - Match any character in brackets
  • [^abc] - Match any character NOT in brackets
  • \w - Match word characters (letters, digits, underscore)
  • \d - Match digits

Examples

Basic text search

grep "error" logfile.txt

Searches for the word "error" in logfile.txt

Case-insensitive search

grep -i "ERROR" logfile.txt

Finds "error", "Error", "ERROR", etc.

Search with line numbers

grep -n "function" program.c
# Output: 15:void function() {
# Output: 42:int main_function() {

Shows line numbers where matches are found

Recursive search in directories

grep -r "TODO" src/
grep -r --include="*.py" "import" project/

Searches in all files within directories, optionally filtering by file type

Count matches

grep -c "error" *.log
# Output: access.log:5
# Output: error.log:23

Counts the number of matching lines in each file

Regular expression patterns

grep "^Error" logfile.txt # Lines starting with "Error"
grep "failed$" logfile.txt # Lines ending with "failed"
grep -E "(error|warning)" log.txt # Lines with "error" OR "warning"
grep "[0-9]\{3\}" file.txt # Lines with 3 consecutive digits

Advanced pattern matching with regular expressions

Useful Pattern Examples

  • Email addresses: grep -E "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
  • IP addresses: grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}"
  • Phone numbers: grep -E "\([0-9]{3}\) [0-9]{3}-[0-9]{4}"
  • URLs: grep -E "https?://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
  • Empty lines: grep "^$"
  • Non-empty lines: grep -v "^$"

💡 Tips and Best Practices

  • Use quotes: Always quote patterns to prevent shell interpretation
  • Escape special characters: Use backslashes for literal dots, brackets, etc.
  • Combine with other commands: Use pipes with sort, uniq, wc for analysis
  • Use --color: Add color highlighting for better visibility
  • Performance: Use -F for fixed strings (faster than regex)
  • Large files: Consider using -m to limit number of matches

See also