grep search text for patterns
grep reads its input line by line and prints every line that matches a pattern. It is the fastest way to find something in logs, source code or any text file, and to narrow a big file down before further processing.
Availability Preinstalled on macOS, Linux and BSD
Syntax
grep [options] pattern [file ...]
Common options
| Option | What it does |
|---|---|
-i | Ignore upper and lower case |
-v | Invert: print lines that do not match |
-n | Show line numbers |
-c | Print only the number of matching lines |
-r | Search every file under a directory |
-l | Print only the names of files that match |
-w | Match whole words only |
-o | Print only the matching part of each line |
-E | Use extended regular expressions (|, +, ? without backslashes) |
Examples
Find errors in a log, any case
grep -i error app.log
Search a project with line numbers
grep -rn "TODO" src/
Count matching lines
grep -c " 404 " access.log
Extract IPv4 addresses
grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' access.log
Use grep with sqx
Filter lines before loading
On a very large JSON Lines file, a quick grep shrinks the input before sqx parses it.
grep '"type":"purchase"' events.jsonl | sqx "SELECT ROUND(SUM(amount), 2) AS revenue FROM stdin"
Or filter with REGEXP inside SQL
sqx supports REGEXP, so you can match patterns on a specific column instead of the whole line.
sqx -f logs=access_log.csv "SELECT ts, request_url FROM logs WHERE user_agent REGEXP '(?i)curl|python'"
Search query results
sqx -f users=users.csv -o csv "SELECT * FROM users" | grep -i acme
Put patterns in single quotes so the shell doesn't interpret characters like *, $ or |.
Full documentation
This page covers the essentials. For every option, see the official documentation:
Or read the manual in your terminal: man grep