英文:
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 separately?
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
For example, 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.
英文:
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
答案1
得分: 0
From OP's comment the following do not work:
grep -o '^/usr.*1[.]8*bin/java>'
^^^^^
grep -o '^/usr.*11[.]0[.]17*bin/java>'
^^^^^
问题在于 8*bin 和 7*bin,它们表示匹配0个或多个字符 8 或 7 后跟字符串 bin;当然,在输入中不存在字符串 .bin、.8bin、.8888bin、.1bin、.17bin 和 .17777bin,因此不会生成任何输出。
尝试将 * 应用于单字符通配符 (.),就像在模式的前面应用的那样 (user.*);所以,对 OP 当前代码稍作修改:
grep -o '^/usr.*1[.]8.*bin/java>'
^
grep -o '^/usr.*11[.]0[.]17.*bin/java>'
^
英文:
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\>'
^
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论