pepero
pepero

Reputation: 7513

ask for explanation of sed regex

I struggle to understand the following two sed regex in a makefile:

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!

Upvotes: 0

Views: 90

Answers (1)

Andreas Louv
Andreas Louv

Reputation: 47099

$$ will be substituted to $ in make files, so the sed expression looks like this:

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

s/// is the substitution command. 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.

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. 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.

* 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.

Upvotes: 4

Related Questions