Home > OS >  Combining multiple regexs in a sed statement
Combining multiple regexs in a sed statement

Time:11-11

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 separator
  • v?: Match 1 spaces followed by an optional v
  • ([^,] ): 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] - 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.
  • Related