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 have path name as below,

  1. abc4/2012
  2. abc4/2013
  3. abc45/2014
  4. abc45/2014

When i grep for 45 it is showing one instance of 4 and for 4 its showing all 45. i need to grep both single digit and double digit separately. please help

share|improve this question
up vote 4 down vote accepted
egrep '([^0-9]4[^0-9]|[^0-9]45[^0-9])'

Does it for me - that is, 4 surrounded by non-digits, or 45 surrounded by non-digits.

share|improve this answer
There are also named classes (at least in GNU grep) in the form of [:class_name:], e.g. [^0-9] can be written as [^[:digit:]] - this can be useful when grepping in non-POSIX locales (and when grepping for something else than numbers obviously). – peterph Dec 19 '13 at 12:00

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.