How to grep for the pattern at the end of the line?

String at the ending of a line can be grep-ed by suffixing a dollar($) symbol to the pattern.

grep pattern$ My-Filename
# ($), dollar symbol

Here is an example

[email protected]:~$ grep host$ /etc/hosts
127.0.0.1	localhost
[email protected]:~$ 

Grep pattern at the ending of the line, case insensitive

[email protected]:~$ grep -i Hosts$ /etc/hosts
# The following lines are desirable for IPv6 capable hosts

Grep output to display the line number in the result

[email protected]:~$ grep -in Hosts$ /etc/hosts
4:# The following lines are desirable for IPv6 capable hosts
[email protected]:~$ 

Here is another example with multiple output lines, grep-ing for lines ending with theĀ character s

[email protected]:~$ grep -in s$ /etc/hosts
4:# The following lines are desirable for IPv6 capable hosts
8:ff02::1 ip6-allnodes
9:ff02::2 ip6-allrouters
[email protected]:~$ 

We have just explored several ways to grep pattern at the beginning of a text file