Shell scripting primer from Apple:
You can use regular expressions to search for strings in a file or a block of text by using the grep command. For example, to look for the word “bar” in the file foo.txt, you might do this:
grep "bar" foo.txt # or cat foo.txt | grep "bar"
Positional anchors allow you to specify the position within a line of text where an expression is allowed to match. There are two positional anchors that are regularly used: caret (^) and dollar ($). When placed at the beginning or end of an expression, these match the beginning and end of a line of text, respectively.
# Expression: /^Mary/ grep "^Mary" < poem.txt
This matches the word “Mary”, but only when it appears at the beginning of a line.