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

How to grep the required string from the result of alternatives –display java using start and ends with?

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

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

/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\>'
                           ^
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