简体   繁体   中英

search with grep for words beginning and ending with

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM