What do the grep options -i, -v, -c, -n and -w do?
-i ignores case, -v inverts the match to show non-matching lines, -c prints only a count of matching lines, -n adds line numbers and -w matches whole words only. They combine, so grep -ivn error file works.
Explain it simply
Think of searching a Hindi-English class attendance list for names. These switches change what "match" means or how results are shown.
-iignore case:grep -i errorfinds Error, ERROR and error.-vinvert: show lines that do not match.grep -v '^#' confhides comment lines.-ccount: prints how many lines matched, not the lines.-nline number: prefix each result with its number, helpful for editing.-wword match:grep -w catfinds "cat" but not "category".
Others worth knowing: -r to search folders, -l to list only file names, -E for extended regex, -A 2 -B 2 for context lines.
-c counts matching lines, not occurrences. If a line has the word three times, it counts once. Use grep -o word file | wc -l to count every occurrence.
Example
printf 'Error: disk\nok\nerror: net\n# note\n' > app.log
grep -ic error app.log
# 2
grep -vn '^#' app.log
# 1:Error: disk
# 2:ok
# 3:error: netWhat to say in the interview
"-i ignores case, -v inverts, -c counts matching lines, -n shows line numbers and -w matches whole words. Note -c counts lines, so for total occurrences I use grep -o with wc -l."
Common mistakes
- Using -c and expecting the number of word occurrences instead of lines.