Mot*_*ead 0 bash grep wildcard special-characters
在bash中,我正在尝试搜索(grep)命令(ntp)的输出,以获取特定的字符串.但是,输出中的一列不断变化.所以对于那一列,它可以是任何角色.
我可能没有正确地做到这一点,但是*我没有像我希望的那样工作.
ntpq -p | grep "10 l * 64 377 0.000 0.000 0.001"
Run Code Online (Sandbox Code Playgroud)
星号正在替换从第二个更改为 - 到1-64的列.
任何帮助将非常感激!
甲*在正则表达式是从一个不同的*在壳通配.以下是来自regex(7)联机帮助页:
An atom followed by '*' matches a sequence of 0 or more matches of the atom.
Run Code Online (Sandbox Code Playgroud)
这意味着在你的正则表达式中,你说"匹配0或更多空格".如果你想匹配任何字符的0或更多,你需要.*.
ntpq -p | grep "10 L .* 64 377 0.000 0.000 0.001"
Run Code Online (Sandbox Code Playgroud)
虽然,您可能希望匹配"任何一个或多个角色":
ntpq -p | grep -E "10 L .+ 64 377 0.000 0.000 0.001"
Run Code Online (Sandbox Code Playgroud)
更好的是,只匹配数字或-:
ntpq -p | grep -E "10 L [[:digit:].\-]+ 64 377 0.000 0.000 0.001"
Run Code Online (Sandbox Code Playgroud)