Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to match this: (tt\d{1,10}) in a file with the file ending txt.

I have tried grep and ack-grep but without results.

ack-grep -G \.txt$ -g "(tt\d{1,10})"
share|improve this question
Can you specify whether these (tt\d{1,10}) are meant to be literals, or a regex? In the latter case you need to use grep w/ -E ... – tink Oct 18 '12 at 21:56
Few command-line regex engines honor \d for [0-9]. Is that what you intend? You can use \{...\} instead of {...} if you use basic regex (no -E). – dubiousjim Oct 18 '12 at 21:57
It looks like the documentation does not specify which type of regex is used. The \d is a PCRE extension (also used by other implementations) that are not found in BRE (default regular grep) or ERE (egrep). In BRE the {} characters must be escaped. The () characters also need to be escaped for grouping in BRE. – jordanm Oct 18 '12 at 21:58
So in short, you could try the pattern tt[0-9]\{1,10\}. – dubiousjim Oct 18 '12 at 21:59
@dubiousjim that pattern worked. But using this ack-grep tt[0-9]\{1,10\} *.nfo command will only search the current dir. How can I get it to search recursive? – Patrik Oct 18 '12 at 22:06
up vote 1 down vote accepted

I use find for this kind of thing, and get it to execute grep on matching files. Using @dubiousjim's regex from above:

find ./ -name "*.txt" -exec grep -Hn --color=auto 'tt[0-9]\{1,10\}' {} \;

Sorry to bloat this, but I felt I should return and explain the options in further detail...
The above find command is essentially two separate commands, rolled into one with the '-exec' option.

This first bit I hope is fairly self-explanatory:-
find ./ -name "*.txt"

But find's -exec sub-command has it's own special syntax. {} is specific to find and its -exec option, as is \;. {} is replaced with the full matching path name, and the \; signifies the end of the command line arguments for the exec'd program, which in this case is grep.

grep -Hn --color=auto 'regex' {} \;

I use these options in an attempt to replicate the appearance of grep -r, by printing with colours, line numbers (-n) and file name (-H) for each matched expression. Note that alias's don't work within commands exec'd by find, which is why I add '--color=auto' manually.

share|improve this answer
Awesome thanks. Here is what I ended up with: find ./ -name "*.txt" -exec grep -o 'tt[0-9]\{1,10\}' {} \; – Patrik Oct 18 '12 at 22:24

With ack-grep, looks like you were looking for

ack-grep -uhoG '\.txt$' 'tt\d{1,10}'

Which would be more or less the equivalent of the find command you "ended up with", which, by the way can be optimised to:

find . -name '*.txt' -exec grep -Eo 'tt[0-9]{1,10}' {} +

to avoid having to run one grep command per txt file.

You may also want to consider adding a -type f to avoid having to search the text in directories or symlinks or devices... (though I'll agree it's rare (except for symlinks) for them to have *.txt name).

Also note that the -o option to grep is GNU specific and GNU grep also has a -r option to recursively search into directories, but beware that older versions of GNU grep descend into symlinks to directories, so using find is generally better/safer if you don't have a recent grep.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.