Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:
enter image description here

But when I test my regex using this website which is a sed playground it works.

>Solution :

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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading