Linux and Command-Line Basics
$ cat words.txt
bat
bt
boat
abet
bit.txt
$ grep '^b.t' words.txtIn the session above, which of the following are the lines printed by the last grep?
- Abat, bit.txt
- Bbat, abet, bit.txt
- 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.