Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Combining multiple regexs in a sed statement

I would like to discover the version number of docker-compose. The format differs per version.

Ex: Running docker-compose –version

docker-compose version 1.29.2, build someId
Docker Compose version v2.12.2

I have separate sed statements that grab the version number, but if possible I’d like to combine them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

echo `docker-compose --version` | sed -nr 's|^docker-compose version (.*)(,.*)|\1|p'
echo `docker-compose --version` | sed -nr 's|^Docker compose version v(.*)|\1|p'

If I or the two regexes together using a pipe, the reference needs to change to either \1 or \3 depending where it’s caught.

Is there a better way using sed?

>Solution :

You can merge the two expressions into

echo `docker-compose --version` | sed -nr 's|^[dD]ocker[ -][cC]ompose version v?([^, ]*).*|\1|p'

Details:

  • ^ – start of string
  • [dD]d or D
  • ocker – a literal string
  • [ -] – a space or hyphen
  • [cC]c or C
  • ompose version – a fixed string
  • v? – an optional v char
  • ([^, ]*) – Group 1: zero or more chars other than space and comma
  • .* – the rest of the string.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading