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

Dean Christian Armada
  • 6,724
  • 9
  • 67
  • 116

4 Answers4

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|'
kvantour
  • 25,269
  • 4
  • 47
  • 72
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".

CWLiu
  • 3,913
  • 1
  • 10
  • 14
1

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

Sumit
  • 852
  • 5
  • 6
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.

nbari
  • 25,603
  • 10
  • 76
  • 131