Examveda
Examveda

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

A. ^pat

B. $pat

C. pat$

D. pat^

E. None of the above

Answer: Option A

Solution(By Examveda Team)

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.

This Question Belongs to Computer Science >> Unix

Join The Discussion

Comments ( 1 )

  1. Ravi Chandra
    Ravi Chandra :
    4 years ago

    Matching the lines that start with a string : The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.

    $ grep "^unix" file.txt
    Output:

    unix is great os. unix is opensource. unix is free os.

Related Questions on Unix