简体   繁体   English

无法找出正则表达式bash或sed或awk

[英]Unable to figure out regex bash or sed or awk

I wanted to split the following jdk-1.6.0_30-fcs.x86_64 to just jdk-1.6.0_30 . 我想将以下jdk-1.6.0_30-fcs.x86_64jdk-1.6.0_30 I tried the following sed 's/\\([az][^fcs]*\\).*/\\1/' but I end up with jdk-1.6.0_30- . 我尝试了以下sed 's/\\([az][^fcs]*\\).*/\\1/'但最终得到jdk-1.6.0_30- I think am approaching it the wrong way, is there a way to start from the end of the word and traverse backwards till I encounter - ? 我认为这是错误的方法,有没有一种方法可以从单词的末尾开始并向后移动直到遇到-

Try doing this : 尝试这样做:

echo 'jdk-1.6.0_30-fcs.x86_64' | sed 's/-fcs.*//'

If using , or , you can do : 如果使用 ,则可以执行以下操作:

var=jdk-1.6.0_30-fcs.x86_64
echo ${var%%-fcs*}
jdk-1.6.0_30

Later solution use parameter expansion , tested on Linux and Minix3 以后的解决方案使用参数扩展 ,在Linux和Minix3上进行了测试

Not exactly, but you can anchor the pattern to the end of the string with $ . 不完全是,但是您可以使用$将模式锚定到字符串的末尾。 Then you just need to make sure that the characters you repeat may not include hyphens: 然后,您只需要确保重复的字符不包含连字符即可:

echo jdk-1.6.0_30-fcs.x86_64 | sed 's/-[^-]*$//'

This will match from a - to the end of the string, but all characters in between must be different from - (so that it does not match for the first hyphen already). 这将从-到字符串的末尾匹配,但是中间的所有字符都必须与-不同(这样它就不会与第一个连字符匹配)。

A slightly more detailed explanation. 稍微详细一点的解释。 The engine tries to match the literal - first. 引擎尝试匹配字面-第一。 That will first work at the first - in the string (obviously). 这将首先在工作第一-字符串(显然)英寸 Then [^-]* matches as many non- - characters as possible, so it will consume 1.6.0_30 (because the next character is in fact a hyphen). 然后[^-]*匹配尽可能多的非-字符,因此它将消耗1.6.0_30 (因为下一个字符实际上是连字符)。 Now the engine will try to match $ , but that does not work because we are not at the end of the string. 现在,引擎将尝试匹配$ ,但这不起作用,因为我们不在字符串末尾。 Some backtracking occurs, but we can ignore that here. 发生了一些回溯,但是我们可以在这里忽略它。 In the end the engine will abandon matching the first - and continue through the string. 最后,引擎将放弃匹配第一个-并继续通过字符串。 Then the engine will match the literal - with the second - . 那么发动机将匹配字面-与第二- Now [^-]* will consume fcs.x86_64 . 现在[^-]*将消耗fcs.x86_64 Now we are actually at the end of the string and $ will match, so the full match (which will be removed is) -fcs.x86_64 . 现在我们实际上位于字符串的末尾,并且$将匹配,因此完全匹配(将被删除的)是-fcs.x86_64

使用cut >>

echo 'jdk-1.6.0_30-fcs.x86_64' | cut -d- -f-2

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

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