Interview Q&A / Linux & Shell

BeginnerQuick answer1 min readUpdated 3 Oct 2026

What do the grep options -i, -v, -c, -n and -w do?

SHORT ANSWER

-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.

  • -i ignore case: grep -i error finds Error, ERROR and error.
  • -v invert: show lines that do not match. grep -v '^#' conf hides comment lines.
  • -c count: prints how many lines matched, not the lines.
  • -n line number: prefix each result with its number, helpful for editing.
  • -w word match: grep -w cat finds "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

bash
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: net

What 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.

#linux#text-processing#grep

Related questions

Practise more Linux & Shell questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Linux & Shell quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.