Linux and Command-Line Basics
$ cat log.txt
INFO start
ERROR disk full
warning: error retry
Error: error twice
INFO done
$ grep -ic error log.txtIn the session above, which of the following is the value printed by the last command?
- A2
- B3
- C4
Show answer and explanation
Answer: B. 3
grep's -i means ignoring the difference between upper and lower case, and -c means printing the "number of lines" that match. The lines containing error regardless of case are the three lines ERROR disk full, warning: error retry and Error: error twice, so the output is 3. error appears twice in the fourth line, but -c counts lines, so it is counted as 1. Other frequently used options are -v to print non-matching lines, -r to search through subdirectories as well and -n to add line numbers.
- A. This is the value without -i. With -i, the lines containing ERROR and Error are counted too.
- B. Correct. Three lines contain error when case is ignored.
- C. This counts the matches (error appears 4 times). -c counts the number of matching lines.