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.
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]–dorDocker– a literal string[ -]– a space or hyphen[cC]–corCompose version– a fixed stringv?– an optionalvchar([^, ]*)– Group 1: zero or more chars other than space and comma.*– the rest of the string.