What are the most useful `grep` flags?
BeginnerConceptText processing: grep, sed, awk, cut, sort, uniq
Answer
| Flag | Purpose |
|---|---|
-i |
ignore case |
-v |
invert match |
-c |
count matching lines |
-n |
show line numbers |
-o |
print only the matched part |
-l / -L |
list files with / without matches |
-r / -R |
recursive (-R follows symlinks) |
-E / -F / -P |
extended regex / fixed string / Perl regex (GNU) |
-w / -x |
whole word / whole line |
-A n -B n -C n |
lines after / before / around |
-q |
quiet, exit status only |
-m n |
stop after n matches |
grep -rnE --include='*.yaml' 'image: .*:latest' k8s/
Exit status: 0 match, 1 no match, 2 error. Under set -e, grep finding nothing exits your script.
More on Text processing: grep, sed, awk, cut, sort, uniq
What is the difference between basic, extended and Perl regular expressions in grep?IntermediateWhat are the core `sed` commands you use in automation?BeginnerWhy does `sed -i` behave differently on macOS and Linux?IntermediateHow do you use variables inside a `sed` expression safely?AdvancedWhat is the structure of an `awk` program?BeginnerHow do you count occurrences per key with `awk` associative arrays?Intermediate