grep ⇒ global regular expressions print
grep uses posix regular expressions you should use grep -P to get perl compatible regular expressions
Find lines containing the exact word “root” in the file users.txt
. Ensure it matches whole words only, not substrings like “rooted” or “rooter”.
grep -w "root" users.txt
Write a command to search for the word “connection” in network.log
, but only show the count of matching lines.
grep -c "connection" network.log
How would you search for lines containing the word “fail” in application.log
, but only show the file name of the files where a match is found (without showing the actual matching lines)?
grep -vP "^\d+$|^.*-->.*$" lesson13.srt | xclip -selection clipboard
Grep Command Options Reference Guide
Basic Pattern Matching Options
-i : Ignore case distinctions in patterns and input data -w : Match only whole words (surrounded by non-word characters) -x : Match only whole lines -v : Invert match (select non-matching lines) -F : Interpret pattern as a fixed string (not a regular expression) -E : Interpret pattern as an extended regular expression -P : Interpret pattern as a Perl regular expression
Output Control Options
-n : Prefix each line of output with line number -H : Print filename with output lines -h : Suppress filename prefix in output -l : Print only names of files containing matches -L : Print only names of files not containing matches -c : Print only count of matching lines per file -o : Show only the matched parts of lines -q : Quiet mode (suppress normal output) -s : Suppress error messages about nonexistent or unreadable files -b : Print byte offset of match -m NUM : Stop after NUM matches
Context Control Options
-B NUM : Print NUM lines of leading context before matching lines -A NUM : Print NUM lines of trailing context after matching lines -C NUM : Print NUM lines of context (before and after matching lines)
File and Directory Handling
-r : Recursively search directories -R : Recursively search directories (follow symbolic links) -d ACTION : Process directories (ACTION: ‘read’, ‘skip’, or ‘recurse’) -D ACTION : Process devices (ACTION: ‘read’ or ‘skip’) -f FILE : Obtain patterns from FILE -e PATTERN : Use PATTERN as the pattern (useful when pattern starts with ’-‘)
Binary File Options
-a : Process binary files as text -I : Process binary files as if not containing matches -U : Process binary files as UTF-8 text -z : Treat input/output data as null-terminated lines
Performance Options
-T : Initial tab alignment -Z : Output null-terminated lines —line-buffered : Flush output on every line —mmap : Use memory-mapped input if possible
Examples of Usage:
-
Case-insensitive search:
grep -i "pattern" file.txt
-
Search whole words only:
grep -w "word" file.txt
-
Show line numbers with matches:
grep -n "pattern" file.txt
-
Recursive search with context:
grep -R -C 2 "pattern" directory/
-
Count matches in file:
grep -c "pattern" file.txt
Common Combinations:
-
Recursive case-insensitive search:
grep -ri "pattern" directory/
-
Search whole words with line numbers:
grep -wn "word" file.txt
-
Extended regex with context:
grep -E -C 2 "pattern1|pattern2" file.txt
-
Quiet recursive search with filename only:
grep -rlq "pattern" directory/
Tips:
- Use -E for extended regular expressions when you need more complex patterns
- Combine -r with -l to find files containing matches
- Use -v with -l to find files NOT containing matches
- When searching binary files, -a or -I might be necessary
- Use -m to limit the number of matches when searching large files
Examples
Command Overview
blkid /dev/sda1 | grep -Po 'UUID="\K[^"]+'
blkid /dev/sda1
: Outputs information about the/dev/sda1
block device, including its UUID, label, type, etc.grep -Po
:-P
: Enables Perl-compatible regular expressions (PCRE), which allow advanced patterns like\K
.-o
: Outputs only the matching parts of the input (not the entire line).
Breaking Down the Regular Expression
-
\K
- “Forget everything matched so far.”
\K
tellsgrep
to drop the part of the match (UUID="
) from the final output. Only the part after\K
will be captured in the result.- Essentially, this prevents
UUID="
from appearing in the output.
-
[^"]+
[^"]
: Matches any character that is NOT a double quote ("
).+
: Matches one or more of the preceding character (in this case, any non-quote characters).- This ensures we capture the UUID value, which consists of alphanumeric characters up to (but not including) the next double quote.
Example Match
Input:
/dev/sda1: LABEL="New Volume" BLOCK_SIZE="512" UUID="F86E68DF6E6897E0" TYPE="ntfs"
Step-by-step matching:
UUID="
: Matches the start of the UUID definition.\K
: Tellsgrep
to forget about theUUID="
part.[^"]+
: MatchesF86E68DF6E6897E0
(everything up to the next"
).
Output:
F86E68DF6E6897E0