On my Linux machine, it isn't clear to me why if I do the following then I don't get only the version string ("1.5.0_32").

# java -version | grep version | awk '{print $NF}'
java version "1.5.0_32"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_32-b05)
Java HotSpot(TM) Server VM (build 1.5.0_32-b05, mixed mode)

Why don't grep or awk work?

Just to show that grep and awk work on other example

# echo ' java version "1.5.0_32" ' | grep version  | awk '{print $NF}'

Try like this:

java -version 2>&1 | grep version  | awk '{print $NF}'

Looks like the output is going to stderr.

Also, grep is not needed:

java -version 2>&1 | awk '/version/{print $NF}'
  • Plus one for the "grep not needed" :) – tink Apr 18 '13 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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