[英]How to print string before specific word in bash?
I want to learn how to extract only the Kernel Version from this specific output:我想了解如何从这个特定的 output 中仅提取 Kernel 版本:
3.10.0-1127.18.2.el7.x86_64Repository rhel-7-server-optional-rpms is listed more than once in the configuration
This is the output that I am aiming for: 3.10.0-1127.18.2.el7.x86_64
这是我的目标 output:
3.10.0-1127.18.2.el7.x86_64
bash: bash:
var="3.10.0-1127.18.2.el7.x86_64Repository rhel-7-server-optional-rpms"
echo "${var%%Repository*}"
See 3.5.3 Shell Parameter Expansion in the manual.参见手册3.5.3 Shell参数扩展。
There are several ways.有几种方法。
A "simple" is using sed with a regex to replace the part that you want to strip. “简单”是使用 sed 和正则表达式来替换您要剥离的部分。
Ex:前任:
echo "3.10.0-1127.18.2.el7.x86_64Repository rhel-7-server-optional-rpms" | sed -E "s/Repository.*//"
3.10.0-1127.18.2.el7.x86_64
To explain the sed
command used: sed -E "s/Repository.*//"
:解释使用的
sed
命令: sed -E "s/Repository.*//"
:
E` is for extended regular expression. E` 用于扩展正则表达式。
And sed syntax substitution is:而 sed 语法替换为:
s/regexp/replacement/
Attempt to match regexp against the pattern space.
尝试将正则表达式与模式空间进行匹配。 If successful, replace that portion matched with replacement
如果成功,则替换与替换匹配的部分
Here we replace the string found by nothing.在这里,我们将找到的字符串替换为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.