[英]Regex using sed and or grep
How can I display the arch and version of queried rpm package using sed or grep? 如何使用sed或grep显示查询的rpm包的arch和版本?
[root@kitchen-vm-centos6-box boot]# rpm -qa | grep kernel-devel
kernel-devel-2.6.32-642.11.1.el6.x86_64
kernel-devel-2.6.32-696.10.2.el6.x86_64
What i need only is: 我只需要:
2.6.32-642.11.1.el6.x86_64
What is missing in my sed? 我的sed缺少什么? =>
sed 's/[^\\.]\\+\\.//'
=>
sed 's/[^\\.]\\+\\.//'
Thanks in advance! 提前致谢!
你也可以使用cut:
rpm -qa | grep kernel-devel | cut -d \- -f 3-4
You can use sed
as this and avoid en extra grep
: 你可以使用
sed
作为这个,并避免额外的grep
:
rpm -qa | sed '/kernel-devel/s/^[^0-9]*//'
2.6.32-642.11.1.el6.x86_64
2.6.32-696.10.2.el6.x86_64
Your sed
removes the first dot after the first "2", because it's matched by the regex you provided. 你的
sed
删除了第一个“2”之后的第一个点,因为它与你提供的正则表达式相匹配。
You can fix easily by making the regex more explicit. 您可以通过使正则表达式更明确来轻松修复。
Other answers already suggested solutions, here's another one using grep
: 其他答案已经提出解决方案,这是另一个使用
grep
:
$ rpm -qa | grep -oP "devel-\K(.*)"
2.6.32-642.11.1.el6.x86_64
2.6.32-696.10.2.el6.x86_64
\\K
tells the engine to pretend that the match attempt started at this position (that's the alternative that Perl suggested for lookbehind). \\K
告诉引擎假装在这个位置开始匹配尝试(这是Perl为lookbehind建议的替代方案)。
You can do it with grep only: 你只能用grep来做:
rpm -qa | grep -P -o '(?<=kernel-devel-).*'
Explanation: 说明:
-o
is match only. -o
仅匹配。 Ie grep will return the matched part only -P
is perl regex mode. -P
是perl regex模式。 It enables lookarounds. (?<=...)
is lookbehind. (?<=...)
是后卫。 Ie stuff before the match. -o
is not going to retain it -o
不会保留它 Of course, sed can help too: 当然,sed也可以提供帮助:
rpm -qa | grep 'kernel-devel' | sed 's/^[^.0-9]*-//g'
Explanation: 说明:
^
matches the start of the string ^
匹配字符串的开头 [^.0-9]
matches the non-dot, non-number characters from the start of the string. [^.0-9]
匹配字符串开头的非点,非数字字符。 This is the part that we don't need. //g
ending of the sed command replaces the matched part with empty string //g
结尾的sed命令用空字符串替换匹配的部分 One in awk: 一个在awk:
$ rpm -qa | awk 'match($0,/^kernel-devel-./){print substr($0,RLENGTH)}'
2.6.32-642.11.1.el6.x86_64
2.6.32-696.10.2.el6.x86_64
Explained: 解释:
match($0,/^kernel-devel-./) { # if the record starts with kernel-devel-[ANYTHING]
print substr($0,RLENGTH) # print starting from the [ANYTHING]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.