Vidyalelo
Computer Science · Q1

UNIX

Engineering and GATE · Computer Science · question 1

Q1

Which symbol will be used with grep command to match the pattern pat at the beginning of a line?

A.
^pat
Answer
B.
$pat
C.
pat$
D.
pat^
E.
None of the above

Answer: Option A

Solution

Answer: Option A
Solution:
In Unix, when using the grep command, the caret (^) symbol is used to match patterns at the beginning of a line.
So, if you use ^pat with grep, it will match all the lines that start with the pattern "pat".
The caret (^) is called a metacharacter in regular expressions and it anchors the search pattern to the beginning of a line.
When you use ^pat, grep will only return lines where the pattern "pat" occurs at the beginning of the line.
This means that if "pat" occurs anywhere else in the line (not at the beginning), it won't be considered a match.
So, to summarize, ^pat is the correct option because it specifically instructs grep to find lines where "pat" appears at the very beginning.