Linux and Command-Line Basics

$ cat words.txt
bat
bt
boat
abet
bit.txt
$ grep '^b.t' words.txt

In the session above, which of the following are the lines printed by the last grep?

  1. Abat, bit.txt
  2. Bbat, abet, bit.txt
  3. Cbat, bt, boat, bit.txt
Show answer and explanation

Answer: A. bat, bit.txt

In a regular expression, ^ means the start of a line, . any single character and $ the end of a line. ^b.t means "b at the start of the line, then any one character, then t", so it matches bat and bit.txt. bt has no character between b and t, and in boat the character two after b is a, so they do not match. In abet the part bet fits b.t, but the line starts with a, so it fails the ^ condition.

  • A. Correct. bat and bit.txt are the lines with b, any one character and t from the start of the line.
  • B. This is the result without ^ (grep 'b.t'). With ^, only lines that start with b match.
  • C. This is the result of reading . like .* (any string of zero or more characters). . matches exactly one character.
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. 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?
  2. 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?
  3. Which of the following is the output of the command above, which extracts columns from a comma-separated line?
  4. Which of the following is the output of the sed command above, which replaces characters?
  5. In the session above, which of the following is printed by the last awk?