2

I'm trying to parse the output of this pip install to get the latest package version.

pip install package==
      ERROR: Could not find a version that satisfies the requirement package== (from versions: 19.2.0, 19.5.0, 19.6.1, 19.7.0, 19.7.1, 19.8.0, 19.9.0)

It should be done using bash commands (sed, awk or grep). I validate the below expression using regexr.com and it highlights all versions of the given output.

(?:(\d+)\.)?(?:(\d+)\.)?(?:(\d+)\.\d+)

But I'm unable to get this regular expression to work with sed/awk.

7
  • Remark: sed, awk and grep should not be called "bash commands". Those tools are independent from the shell invoking it, they existed before bash and will still exist after bash will be gone and forgotten.
    – Philippos
    Sep 12, 2019 at 14:15
  • Any hint why bash can disappear, or it's a joke ? Sep 12, 2019 at 14:16
  • 2
    Validating an expression with some online tool like regexr.com only proves that that expression behaves as desired in that online tool. It proves nothing about how it'll behave in any command-line tool such as awk, sed, or grep for example. To write a regexp for a given tool you need to understand which regexp variant(s) (BRE, ERE, PCRE) that specific tool understands given which options and between which delimiters and with which extensions.
    – Ed Morton
    Sep 12, 2019 at 14:19
  • There's still some people (devs, sysOps...) knowing zsh, but want to keep bash as default shell ;) Sep 12, 2019 at 14:24
  • 1
    Moreover, PCRE is a subset of Perl's regex, so you can add it : BRE, ERE, PCRE, Perl) ;) Used the online tool only to explain the regex Sep 12, 2019 at 14:26

3 Answers 3

1

In a less complicated manner, if you are just after the latest version of pip you can do this with cut.

Pip show pip | grep Version: | cut -d' ' -f2

will result in: 19.9.0

0

Using only GNU and regex engine :

Command line

pip ... |& grep -oP '.*\b\K\d+\.\d+\.\d+\b'

Output

19.9.0

Explanations about the regex

Check regex101 explanations

1
  • Take care, this prints the intended package version plus 19.2.3 for me due to pip telling You are using pip version 19.0.3, however version 19.2.3 is available.
    – pLumo
    Sep 12, 2019 at 15:13
0
$ echo 'ERROR: Could not find a version that satisfies the requirement package== (from versions: 19.2.0, 19.5.0, 19.6.1, 19.7.0, 19.7.1, 19.8.0, 19.9.0)' | \
    sed -e 's/^.*versions: //; s/, /\n/g; s/)//'  | \
    sort -V | tail -n1
19.9.0

This requires a version of sort that understands the -V (aka --version-sort) option. GNU sort and FreeBSD sort do. I don't know if any others do.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .