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
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 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
Searches for the word "error" in logfile.txt
Case-insensitive search
Finds "error", "Error", "ERROR", etc.
Search with line numbers
# Output: 15:void function() {
# Output: 42:int main_function() {
Shows line numbers where matches are found
Recursive search in directories
grep -r --include="*.py" "import" project/
Searches in all files within directories, optionally filtering by file type
Count matches
# Output: access.log:5
# Output: error.log:23
Counts the number of matching lines in each file
Regular expression patterns
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