Reputation: 29441
How to grep something which begins and end with a character
ABC-0
ABC-1
ABC-10
ABC-20
I wanto to grep -v for ABC-0 and ABC-1
Upvotes: 13
Views: 55420
Reputation: 6456
If you are trying to match words use \b
as the word delimiter, like this:
\b[A-Za-z]-\d+\b
from this reference:
\b
- Matches at the position between a word character (anything matched by\w
) and a non-word character (anything matched by[^\w]
or\W
) as well as at the start and/or end of the string if the first and/or last characters in the string are word characters.
Upvotes: 11
Reputation: 1504062
It's not clear what you mean. If you want a character at the start and double digits at the end, you could use
^[A-Za-z].*\d\d$
If you only want a hyphen and then a single digit, use:
^[A-Za-z].*-\d$
If you don't care how many digits there are (one or more), but there has to be a hyphen, use:
^[A-Za-z].*-\d+$
If none of those are what you want, please give more information... the first sentence of your question doesn't really tally with the rest.
Upvotes: 10
Reputation: 9401
For your example
egrep -v "^(ABC)-(0|1)$"
is the answer. For the common case, please look at Jon's answer
^
marks the start of the pattern, $
the end. |
means or
Upvotes: 5