Introduction
Anchors help you find patterns only at the start or end of text. They make searches exact and clear.
pattern_start='^pattern' pattern_end='pattern$'
grep '^Hello' file.txtgrep 'world$' file.txtgrep '^Error$' file.txt#!/bin/bash # Create a sample file echo -e "Hello world\nHello there\nGoodbye world\nError" > sample.txt # Find lines starting with Hello echo "Lines starting with 'Hello':" grep '^Hello' sample.txt # Find lines ending with world echo "\nLines ending with 'world':" grep 'world$' sample.txt # Find lines exactly 'Error' echo "\nLines exactly 'Error':" grep '^Error$' sample.txt
^ represent in a bash regular expression?^^ anchor matches the start of a line in bash regex.done?$ anchor$ anchor matches the end of a line in regex.done$ matches lines where 'done' is at the end.file.txt contains the lines:apple banana apple pie pineapple apple
grep '^apple$' file.txt^apple$grep 'Error^' logfile.txt^ anchor must be at the start of the pattern to match line start.^Error to match lines starting with 'Error'. Placing ^ after 'Error' is invalid.data.txt that start with a digit and end with a period. Which grep pattern correctly uses anchors to do this?^[0-9] matches lines starting with any digit.\.$ to match a literal '.' at the end. The dot must be escaped to avoid matching any character.^[0-9].*\.$ matches lines starting with digit, any characters, ending with a period.Pass the test and this pattern moves unknown