grep and Regular Expressions
Challenge Gallery
Quick Reference
grep flags:
| Flag | What It Does |
|---|---|
-i |
Case-insensitive search |
-r |
Recursive (search directories) |
-n |
Show line numbers |
-c |
Count matching lines |
-v |
Invert match (non-matching lines) |
-l |
Show only filenames with matches |
-E |
Extended regex (enables +, ?, \|, ()) |
-w |
Match whole words only |
Regex metacharacters (basic):
| Pattern | Matches |
|---|---|
. |
Any single character |
* |
Zero or more of preceding element |
^ |
Start of line |
$ |
End of line |
[abc] |
Any one of a, b, or c |
[0-9] |
Any digit |
[^abc] |
Any character NOT a, b, or c |
Extended regex (grep -E):
| Pattern | Matches |
|---|---|
+ |
One or more of preceding element |
? |
Zero or one of preceding element |
\| |
Alternation (or) |
() |
Grouping |
grep vs. find:
| Tool | Searches | Example |
|---|---|---|
grep |
File contents | grep -rn "malloc" . |
find |
File names/attributes | find . -name "*.c" |
How It Works
Basic Usage
# Search for a string in a file
student@ubuntu:~/cscd240$ grep "printf" hello.c
printf("Hello, World!\n");
# Case-insensitive, with line numbers
student@ubuntu:~/cscd240$ grep -in "include" hello.c
1:#include <stdio.h>
# Recursive search for TODOs in a project
student@ubuntu:~/cscd240$ grep -rn "TODO" .
./lab2/main.c:15: // TODO: implement input validation
./lab3/utils.c:8: // TODO: handle edge case
# Show lines that do NOT match (strip comments)
student@ubuntu:~/cscd240$ grep -v "^#" config.txt
server_port=8080
max_connections=100
student@ubuntu:~/cscd240$ grep "printf" hello.c
printf("Hello, World!\n");
# Case-insensitive, with line numbers
student@ubuntu:~/cscd240$ grep -in "include" hello.c
1:#include <stdio.h>
# Recursive search for TODOs in a project
student@ubuntu:~/cscd240$ grep -rn "TODO" .
./lab2/main.c:15: // TODO: implement input validation
./lab3/utils.c:8: // TODO: handle edge case
# Show lines that do NOT match (strip comments)
student@ubuntu:~/cscd240$ grep -v "^#" config.txt
server_port=8080
max_connections=100
grep in Pipelines
# Find running processes
student@ubuntu:~$ ps aux | grep "gcc"
student 1234 0.0 0.1 12340 1024 pts/0 S 09:15 0:00 gcc hello.c
# Which .c files use malloc?
student@ubuntu:~/cscd240$ grep -rl "malloc" *.c
utils.c
main.c
# Search history
student@ubuntu:~$ history | grep "chmod"
55 chmod 755 hello
89 chmod u+x script.sh
student@ubuntu:~$ ps aux | grep "gcc"
student 1234 0.0 0.1 12340 1024 pts/0 S 09:15 0:00 gcc hello.c
# Which .c files use malloc?
student@ubuntu:~/cscd240$ grep -rl "malloc" *.c
utils.c
main.c
# Search history
student@ubuntu:~$ history | grep "chmod"
55 chmod 755 hello
89 chmod u+x script.sh
Regular Expressions
# Lines starting with # (comments)
student@ubuntu:~$ grep "^#" hello.c
#include <stdio.h>
# Lines ending with a semicolon
student@ubuntu:~$ grep ";$" hello.c
printf("Hello, World!\n");
return 0;
# Extended regex: match "int" or "char" or "double"
student@ubuntu:~$ grep -E "(int|char|double)" program.c
int main(void) {
int count = 0;
char name[50];
double average;
student@ubuntu:~$ grep "^#" hello.c
#include <stdio.h>
# Lines ending with a semicolon
student@ubuntu:~$ grep ";$" hello.c
printf("Hello, World!\n");
return 0;
# Extended regex: match "int" or "char" or "double"
student@ubuntu:~$ grep -E "(int|char|double)" program.c
int main(void) {
int count = 0;
char name[50];
double average;
Common Pitfalls
- Unquoted patterns –
grep [0-9] filelets the shell interpret[0-9]as a glob. Always usegrep '[0-9]' file. grep "int"matches too much – Catches “printf”, “pointer”, “integer”. Usegrep -w "int"for whole-word match orgrep "^int"for lines starting with int.-ccounts lines, not occurrences – A line with “error” three times counts as 1 withgrep -c.- Forgetting
-Efor extended regex –+,?,|, and()needgrep -E. Without it, they’re treated as literal characters. - grep finds itself in
psoutput –ps aux | grep pythonshows the grep process too. Usegrep '[p]ython'to avoid it.