7.3. Matching a string or character at the end of a line with $

You could say that the opposite of the ^ is the $, which lets us match a string or character at the end of the line. The dollar sign goes at the end of the word you want to match. Suppose we wanted to match all lines that end with the word trouble, this would do it:

trouble$ 

Similarly, using ld$ would return all words ending with ld at the end of each line.

If we wanted to match all empty lines, we could do this as follows:

^$ 

This says start at the beginning of the line, and end at the end of the line. As there is no pattern involved it will return only blank lines.

If we wanted to return lines that contained only one character, we could do this:

^.$ 

Unlike the blank lines example, ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.