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.7. Matching a specific number of occurrences with \{ \}

Using the * allows us to match any number of matches, but if we want to match only a certain number of occurrences, that’s where \{ \} comes in. There are actually three options in using this pattern match, which are:

pattern \{n\} Match n occurrences of the pattern
pattern \{n,\} Match at least n occurrences of the pattern
pattern \{n,m\} Match occurrences of the pattern between n and m, where n and m are integer numbers between 0 and 255

Looking at the first examples, suppose we wanted to match only three occurrences of the letter AA, but ending in the letter B, this would do it:

A\{2\}B 

This would give me AAB in a match.

If we wanted to match at least four occurrences of the letter ...

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