How to use the regular pattern operator {m,n} in grep

command linegrepregular expression

I have a text file, from which I need to select only those lines, which include the string "tt". So I tried this on the command line:

grep "t{2}" textfile

Allthough I know the textfile contains words like "rotten" "litter"
the grep command shows no lines, the exit status however is 1.

I tried another regular pattern:

grep "a.*x" textfile

for this one it works.

Best Answer

By default, grep uses Basic Regular Expressions, you need to escape the braces to make grep match multiple characters:

grep 't\{2\}' textfile

Alternatively, you can use the -E option (or -P option for GNU grep, which uses Perl Compatible Regular Expressions) making grep use Extended Regular Expressions, which can use braces without escaping them:

grep -E 't{2}'
Related Question