1

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

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

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Node.JS
  • 1,042
  • 6
  • 44
  • 114
  • 1
    You could try following command to get its version: `scala -version 2>&1 | sed 's/.*version \([0-9]*\.[0-9]*\.[0-9]*\).*/\1/'`. Check link https://stackoverflow.com/a/818284/5866580 for understanding `2>&1` in detailed manner. I have also fixed your regex to get all 3 parts of version of scala. – RavinderSingh13 Jan 12 '22 at 05:37
  • @RavinderSingh13 it worked. Thank you. But why? – Node.JS Jan 12 '22 at 05:38
  • I have already updated my comment, kindly do check it once. – RavinderSingh13 Jan 12 '22 at 05:39
  • It doesn't look to me complete duplicate since I fixed your standard input/output along with your regex, for time being I have posted it in comments. – RavinderSingh13 Jan 12 '22 at 05:42

1 Answers1

2

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'
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93