Issue
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 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 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
Answered By - anubhava Answer Checked By - Katrina (WPSolving Volunteer)