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)?

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.

  • 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
  • 1
    @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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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