Why sed command doesn't work trying to extract scala version number
I am trying to extract Scala major version using sed
but it returns the same string back. I am not sure.
scala version:
scala -version
> Scala code runner version 2.13.7 -- Copyright 2002-2021, LAMP/EPFL and Lightbend, Inc.
Tried sed
command is:
scala -version | sed 's/.*version \([0-9]*\.[0-9]*\).*/\1/'
> Scala code runner version 2.13.7 -- Copyright 2002-2021, LAMP/EPFL and Lightbend, Inc.
Screenshot:
But when I test my regex using this website which is a sed playground it works.
You have 2 problems in your tried/attempted code. 1st: Is you need to send scala --version
output to 2>&1
(check link https://stackoverflow.com/a/818284/5866580 for understanding it better). 2nd: your regex is NOT catching exact version of scala, so you need to enhance it a bit, please try following sed
command for same.
scala -version 2>&1 | sed 's/.*version \([0-9]*\.[0-9]*\.[0-9]*\).*/\1/'
OR as an alternative you could use following sed
command also:
scala -version 2>&1 | sed -E -n 's/.*version\s+([0-9]+)(\.[0-9]+){2}.*/\1\2/p'