0

The command below will return the corresponding strings

$ docker-compose exec postgres postgres --version
postgres (PostgreSQL) 10.4 (Debian 10.4-2.pgdg90+1)

I am trying to get the postgresql version but when I tried, the Debian version and other numbers are included like

$ pg_version=$(docker-compose exec postgres postgres --version | sed 's/[^0-9.]//g')
10.410.42.901

I am wondering how to get the 10.4 only

2
  • You could hack it at spaces etc. ... but I'd use one of the other methods (like SHOW server_version): stackoverflow.com/questions/13733719/… Commented Jul 30, 2018 at 2:30
  • It still showed 10.4 (Debian 10.4-2.pgdg90+1) Commented Jul 30, 2018 at 2:50

4 Answers 4

2
  • awk -v RS=" " '/^[0-9.]+$/{print; exit}'
  • grep -oE '[.0-9]+' | head -1
  • tr ' ' '\n' | grep -oE -m 1 '[.0-9]+'
  • sed 's|^[^0-9.]*\([0-9.]\+\).*|\1|'
Sign up to request clarification or add additional context in comments.

Comments

1

Modify the sed as followed would help,

sed -E 's/.*PostgreSQL[^0-9.]+([0-9.]*).*/\1/'

\1 would only match to the version number right behind "PostgreSQL".

Comments

1

pg_version=$(docker-compose exec postgres postgres -V | grep -oE '[.0-9]+' | head -1)

4 Comments

10.4 10.4. This value returned. How to return only one 10.4?
Since it finds the version two times, put head -1 to print first one.
@Sumit : I think the egrep pattern [.0-9]+ would be sufficient in this case. BTW, it's better to write it as grep -oE, since egrep is deprecated.
@user1934428 Yeah, Thanks
0

You could use grep with something like this:

$ grep -oP "PostgreSQL.\s\K.+?(?=\s)"

For example:

$ echo "postgres (PostgreSQL) 10.4 (Debian 10.4-2.pgdg90+1)" | grep -oP "SQL.\s\K.+?(?=\s)"
10.4

The \K can be read as excluding everything to the left *SQL)<space> before it and return only the right part .+?(?=\s) until and space \s is found.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.