简体   繁体   English

用grep搜索开头和结尾的单词

[英]search with grep for words beginning and ending with

How to grep something which begins and end with a character 如何grep以字符开头和结尾的东西

ABC-0
ABC-1
ABC-10
ABC-20

I wanto to grep -v for ABC-0 and ABC-1 我想要为ABC-0和ABC-1 grep -v

If you are trying to match words use \\b as the word delimiter, like this: 如果您要匹配单词,请使用\\b作为单词分隔符,如下所示:

\\b[A-Za-z]-\\d+\\b \\ 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. \\b - 在单词字符(与\\w匹配的任何内容)和非单词字符(由[^\\w]\\W匹配的任何内容)之间以及字符串的开头和/或结尾处的位置匹配如果字符串中的第一个和/或最后一个字符是单词字符。

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 对于常见情况,请看Jon的答案

^ marks the start of the pattern, $ the end. ^标记模式的开始, $结束。 | means or or

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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