A reminder of the parameters I use most frequently with grep
.
Bash
grep "pattern" file.txt
# searches for pattern in file
grep -i "pattern" file.txt
# ignores case (case-insensitive search)
grep -R "pattern" /path
# recursively searches all files in the given path
grep -n "pattern" *.txt
# shows matching lines with line numbers
grep -v "^#" file.txt
# shows all lines that do not start with # (filters out comments)
grep -c "pattern" file.txt
# returns the number of matches; if given a directory, counts per file
grep -l "pattern" /path
# lists each file that contains at least one match (only once per file)
grep -w "pattern" file.txt
# matches the pattern as a whole word
grep --color=auto "pattern" file.txt
# highlights matches in color
grep -A 2 "pattern" file.txt
# shows 2 lines **after** each match
grep -B 2 "pattern" file.txt
# shows 2 lines **before** each match
grep -C 2 "pattern" file.txt
# shows 2 lines of context before and after each match
grep -E "pattern1|pattern2|pattern3" file.txt
# shows all lines that contain at least one of the 3 patterns
grep "pattern1" file.txt | grep "pattern2" | grep "pattern3"
# shows only lines that contain **all 3** patterns (logical AND)
awk '/pattern1/ && /pattern2/ && /pattern3/' file.txt
# alternative way for AND search, case-sensitive
grep -Rin "pattern" /path
# recursive, case-insensitive search with line numbers
grep -Ril "pattern" /path
# recursive, case-insensitive, lists matching filenames only (once)
Leave a Reply
You must be logged in to post a comment.