Introduction
You use grep with basic regex to find text patterns in files or outputs quickly and easily.
grep 'pattern' filenamegrep 'cat' file.txtgrep '^Hello' file.txtgrep 'end$' file.txtgrep 'cat\|dog' file.txt# Create a sample file cat << EOF > sample.txt cat caterpillar dog catalog end Hello world Hello there EOF # Find lines starting with 'cat' grep '^cat' sample.txt # Find lines ending with 'og' grep 'og$' sample.txt # Find lines containing 'cat' or 'dog' grep 'cat\|dog' sample.txt
^ mean in a basic regex pattern used with grep?^ is used to anchor the pattern to the start of a line in basic regex.$ matches the end of a line, so caret is specifically for the start.grep?\|, not just |.| without escape, which is invalid in basic regex. grep 'cat\|dog' file.txt correctly escapes it as \|. Options C and D use wrong characters.animals.txt with these lines:cat caterpillar dog catalog dogma
grep '^cat$' animals.txt?^cat$ matches lines that start and end exactly with "cat" (no extra characters).fruits.txt using basic regex with grep. Which command uses incorrect syntax for the OR operation?\|. Using | alone treats it as a literal | character, not as an OR operator.| without escape, treating | as literal, not OR. Options A, B, and C correctly escape OR operator.data.txt that start with "error" or end with "fail" using basic regex with grep. Which command correctly does this?^error OR fail$.\|. So pattern is ^error\|fail$. grep '^error\|fail$' data.txt matches this exactly.$. grep '^error\|fail' data.txt misses anchor on "fail".Pass the test and this pattern moves unknown