student@ubuntu:~$
shell 3/5 35 XP

grep and Regular Expressions

0%

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

Terminal
# 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

grep in Pipelines

Terminal
# 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

Regular Expressions

Terminal
# 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;

Common Pitfalls

  • Unquoted patternsgrep [0-9] file lets the shell interpret [0-9] as a glob. Always use grep '[0-9]' file.
  • grep "int" matches too much – Catches “printf”, “pointer”, “integer”. Use grep -w "int" for whole-word match or grep "^int" for lines starting with int.
  • -c counts lines, not occurrences – A line with “error” three times counts as 1 with grep -c.
  • Forgetting -E for extended regex+, ?, |, and () need grep -E. Without it, they’re treated as literal characters.
  • grep finds itself in ps outputps aux | grep python shows the grep process too. Use grep '[p]ython' to avoid it.

Unlocks

Complete this skill to see what it unlocks.