Which symbol will be used with grep command to match the pattern pat at the beginning of a line?
-
^pat
- $pat
- pat$
-
pat^
To match the pattern "pat" at the beginning of a line using the grep command, the symbol ^ will be used.
Now let's go through each option and explain why it is right or wrong:
A. ^pat: This option is correct. The ^ symbol in a regular expression is used to match the pattern at the beginning of a line. Therefore, ^pat will match the pattern "pat" at the beginning of a line.
B. \$pat: This option is incorrect. The $ symbol in a regular expression is used to match the pattern at the end of a line. Therefore, \$pat will match the pattern "pat" at the end of a line, not at the beginning.
C. pat$: This option is incorrect. The $ symbol in a regular expression is used to match the pattern at the end of a line. Therefore, pat$ will match the pattern "pat" at the end of a line, not at the beginning.
D. pat^: This option is incorrect. The symbol ^ does not have any special meaning when it appears after the pattern. Therefore, pat^ will match the literal string "pat^" rather than the pattern "pat" at the beginning of a line.
The Answer is: A. ^pat
In grep (and regular expressions generally), the caret ^ anchors a pattern to the start of a line, so ^pat matches lines beginning with pat. The dollar sign $ is the corresponding end-of-line anchor, which is why pat$ matches lines ending with pat instead. Placing the anchor after the pattern (pat^) or using $ before it doesn't have the intended anchoring meaning.