I wanted to fetch two outputs from the alternatives --display java list
, one is /usr/lib/jvm/jdk-1.8-oracle-x64/bin/java and the other is /usr/java/jdk-11.0.17/bin/java I use the below command
alternatives --display java | grep -o '^/usr.*bin/java\>'
and I get output
/usr/lib/jvm/jdk-1.8-oracle-x64/bin/java
/usr/java/jdk-11.0.17/bin/java
To be specific, this server has jdk 8 and jdk 11 installed. What additional regex can be added to the above command to get the output seperately ?
I need help with the above command to get the single output as
/usr/lib/jvm/jdk-1.8-oracle-x64/bin/java
when trying to fetch jdk-1.8
and
/usr/java/jdk-11.0.17/bin/java
when trying to fetch jdk-11.0.17
eg when i run alternatives --display java | grep -o '^/usr.*1[.]8*bin/java\>' , the output should be /usr/lib/jvm/jdk-1.8-oracle-x64/bin/java and when i run alternatives --display java | grep -o '^/usr.*11[.]0[.]17*bin/java\>' , the output should be /usr/java/jdk-11.0.17/bin/java . Unfortunately , I get no result when i run the command in the example
>Solution :
From OP’s comment the following do not work:
grep -o '^/usr.*1[.]8*bin/java\>'
^^^^^
grep -o '^/usr.*11[.]0[.]17*bin/java\>'
^^^^^
The problem here is the 8*bin and 7*bin which says to match on 0 or more of the characters 8 or 7 followed by the string bin; of course, the strings .bin, .8bin, .8888bin, .1bin, .17bin and .17777bin do not occur in the input so no output is generated.
Try applying the * to the single character wildcard character (.), as is done at the front of the pattern (user.*); so, slight modification to OP’s current code:
grep -o '^/usr.*1[.]8.*bin/java\>'
^
grep -o '^/usr.*11[.]0[.]17.*bin/java\>'
^