Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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 was wondering how can I grep an expression of the type


In other words, I want to target lines that have the following pattern:

 anything + stringA + anything + [number]

For example these strings would match:

this is a test stringA because_[4]
nothing really stringA[5]

these strings would not match:

something else [7]

How can I do this with grep? (or grep -e)?

share|improve this question
up vote 4 down vote accepted
grep 'stringA.*\[[[:digit:]]\]'

.* matches any character 0 or more times. \[ and \] escape their respective characters, which otherwise have special meaning. [:digit:] (usually) expands to 0123456789.

share|improve this answer
Thanks Chris, why do I need three square brackets? (in your explanation you describe two levels of bracketing: \[ \] and [:digit:] – Amelio Vazquez-Reina Feb 6 '12 at 23:45
@intrpc Firstly, the literal [. Then, [ (character class), which matches any character contained. Then, [:digit:], which expands to a list of digits. Then, the reverse. – Chris Down Feb 6 '12 at 23:50

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.