简体   繁体   English

询问sed正则表达式的解释

[英]ask for explanation of sed regex

I struggle to understand the following two sed regex in a makefile: 我很难理解makefile中的以下两个sed正则表达式:

sw_version:=software/module/1.11.0

sw:= $(shell echo $(sw_version) | sed -e 's:/[^/]*$$::;s:_[^/]*$$::g')
// sw is "software/module"

version:= $(shell echo $(sw_version) | sed -e 's:.*/::g;s/-.*$$//')
// version is "1.11.0"

I really appreciate a detailed explanation. 我非常感谢详细的解释。 Thanks! 谢谢!

$$ will be substituted to $ in make files, so the sed expression looks like this: $$将在make文件中替换为$ ,因此sed表达式如下所示:

sed -e 's:/[^/]*$::;s:_[^/]*$::g'

s/// is the substitution command. s///是替换命令。 And the delimiter doesn't need to be / in your case it's a colon ( : ): 而分隔符不必是/你的情况这是一个冒号( : ):

s:/[^/]*$::;
s:_[^/]*$::g

It works with matching pattern and replacing with replacement: 它适用于匹配模式,并用替换替换:

s/pattern/replacement/

; is a delimiter to use multiply commands in the same call to sed. 是在对sed的同一调用中使用乘法命令的定界符。

So basically this is two substitutions one which replaces /[^/]*$ another which replaces _[^/]*$ with nothing. 因此,基本上这是两个替换,一个替换/[^/]*$另一个替换为_[^/]*$

[...] is a character class which will match what ever you stick in there one time. [...]是一个角色类,它将一次匹配您在其中粘贴的内容。 eg: [abc] will match either a or b or c. 例如: [abc]将匹配a或b或c。 If ^ is in the beginning of the class it will match everything but what is in the class, eg: [^abc] will match everything but a and b and c. 如果^在类的开头,它将匹配所有内容,但不包括类中的所有内容,例如: [^abc]将匹配除了a和b以及c之外的所有内容。

* will repeat the last pattern zero or more times. *将最后一个模式重复零次或多次。

$ is end of line $是行尾

Lets apply what we know to the examples above (read bottom up): 让我们将我们所知道的应用于上面的示例(自下而上):

s:/[^/]*$::;
#^^^^^ ^^ ^^
#||||| || |Delimiter
#||||| || Replace with nothing
#||||| |End of line
#||||| Zero or more times
#||||Literal slash
#|||Match everything but ...
#||Character class
#|Literal slash
#Delimiter used for the substitution command

/[^/]*$ will match literal slash ( / ) followed by everything but a slash zero or more times at end of line. /[^/]*$将匹配文字斜杠( / ),然后在行尾匹配零斜杠零次或多次。

_[^/]*$ will match literal underscore ( _ ) followed by everything but a slash zero or more times at end of line. _[^/]*$将匹配文字下划线( _ ),然后除行末尾的斜杠零次或多次外均匹配。

That was the first, the second is left as an exercise. 那是第一个,第二个作为练习。

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

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