O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required