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?
CodePudding user response:
You may use this single sed to grab version number from for both lines shown in question:
sed -E 's/^[dD]ocker[- ][cC]ompose version v?([^,] ).*/\1/'
1.29.2
2.12.2
Breakdown:
^: Start[dD]ocker[- ][cC]ompose version: Match starting string with mixed cases letters and hyphen/space separatorv?: Match 1 spaces followed by an optionalv([^,] ): Match 1 of any char that is not comma and capture in group #1.*: Match everything till end\1: Back-reference to capture group #1
CodePudding user response:
Bash has built-in regex support ([[ $string =~ $regex ]] stores match group contents in the array BASH_REMATCH), so there's no need for sed here. The following is more verbose, but faster to run since there's no need to spin up any external executable:
dockerComposeVersion=$(docker-compose --version)
dcVersionRegex='[Dd]ocker[ -]compose version v?([^,] )'
if [[ $dockerComposeVersion =~ $dcVersionRegex ]]; then
echo "docker-compose version is ${BASH_REMATCH[1]}"
else
echo "Unable to get docker-compose version" >&2
fi
CodePudding user response:
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.
