tags:

views:

327

answers:

5

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

+2  A: 

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.

Jon Skeet
You forgot the quantifier for `[A-Za-z]`.
Gumbo
Or rather, I forgot other things after it - we don't know whether A00-10 would be acceptable. Fixed though, thanks.
Jon Skeet
+1  A: 

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

tuergeist
A: 

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.

João Portela
A: 

try using a^h*hgy^jj

elisha
A: 

uy8hg75^%u*^h&yh7

elisha