0

I have a file with many lines, with lines such as so:

PHP_VERSION="5.3.12"
NPM_VERSION="1.10"

From a linux command line, how would I take out just the version number? For this example, how would I get an output like 5.3.12 if for say I wanted the PHP version out of the given input file.

2
  • I modified my question to be less opinion based. I clearly don't know how to do it and need guidance.
    – Steven Lu
    Commented Mar 21, 2014 at 19:52
  • You can easily cobble something together with sed(1)... you'd need to do sed -ne "s;${PKG}_VERSION=;;p" YourFileHere
    – vonbrand
    Commented Mar 21, 2014 at 22:47

2 Answers 2

3

Your trying to get it with bash?

. /path/to/file
echo $PHP_VERSION

?

3
  • 1
    Rather than source the entire file, I'd evaluate just the line matching PHP_VERSION with eval "$(grep PHP_VERSION /path/to/file)".
    – chepner
    Commented Mar 21, 2014 at 19:50
  • Thanks, but I don't have power to evaluate that script.
    – Steven Lu
    Commented Mar 21, 2014 at 20:03
  • Pardon? Would you mind explaining this in more detail? Commented Mar 22, 2014 at 13:23
2

I would use something like:

var="$(grep -F -m 1 '$variable =' file)"; var="${var#*\'}"; var="${var%\'*}"

Reference: BASH shell use regex to get value from file into a parameter

1
  • 1
    Thanks, the reference you gave me helped me a bunch. I was able to create the command which parsed out the version number. sed -ne "s/PHP_VERSION= *['\"]\([^'\"]*\)['\"].*/\1/p"
    – Steven Lu
    Commented Mar 21, 2014 at 20:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.