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

In the session above, which of the following is the value printed by the last command?

  1. A2
  2. B3
  3. 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.
Practice this topic Back to topic

Books on Linux and Command-Line Basics Ad

As an Amazon Associate I earn from qualifying purchases.

More Linux and Command-Line Basics questions

  1. In the session above, which of the following are the lines printed by the last grep?
  2. The choices omit the leading spaces of the line. In the session above, which of the following is the line printed by the last pipeline?
  3. You want to keep showing lines on the screen as they are appended later to the server log file /var/log/app.log. Which of the following commands is appropriate?
  4. Which of the following is the output of the command above, which extracts columns from a comma-separated line?
  5. Which of the following is the output of the sed command above, which replaces characters?