考虑以下示例,其中使用 grep
以二进制模式进行搜索:
$ echo "test blabla test ertytey" | grep -ao tes
tes
tes
我可以在搜索模式中添加一个“点”,以“匹配任何字符”并在匹配后显示下一个字符:
$ echo "test blabla test ertytey" | grep -ao tes.
test
test
...或更多点,以匹配后续字符:
$ echo "test blabla test ertytey" | grep -ao tes...
test b
test e
现在假设我想在匹配后匹配多个字符(说 30,不:3);我从 man grep
中读到:{n} The preceding item is matched exactly n times. 。所以我尝试:
$ echo "test blabla test ertytey" | grep -ao tes.{3}
$
...没有任何反应;
$ echo "test blabla test ertytey" | grep -ao tes.\{3\}
$
...没有任何反应;
$ echo "test blabla test ertytey" | grep -ao tes[.]\{3\}
$
...没有任何反应;
$ echo "test blabla test ertytey" | grep -ao tes\[.\]\{3\}
$
...没有任何反应。
有什么想法可以在 grep 的二进制模式下匹配“任何字符”(点)给定次数的正确语法吗?
提前非常感谢您的回答,
干杯!
编辑:正如 @aix 的回答指出的那样,最初我错误地指定了 30 个字符进行搜索,而示例中的字符串不够长:) 现在已将其更改为更合理的计数 3 : )
最佳答案
首先,{30}
恰好匹配 30 个字符,而您的字符串不够长。
其次,您的 grep 可能需要 -E
标志来启用扩展语法(我的就是这样)。
以下内容对我有用:
$ echo "test blabla test ertytey" | grep -aoE 'tes.{20}'
test blabla test ertyte
关于linux - Grep:二进制模式下的重复字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7939780/