linux - Grep:二进制模式下的重复字符?

标签 linux grep

考虑以下示例,其中使用 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/

相关文章:

linux - Grep 内部从 ls 和 head 返回的文件

python - 如何将程序输出的数字以空格分隔放入 Bash shell 脚本中的数组中?

c++ - NIC的TX/RX buffer中的数据是如何传输到dpdk ring buffer而不是kernel ring buffer的?

linux - grep 匹配模式的文件名并 move 到所需的文件夹

linux - 从 PCAP 中提取信息

linux - shell脚本过滤du并通过子文件夹中文件内的字符串查找

linux - 如何在 grep 中显示搜索词

c++ - v4l2 很简单的例子

linux - Zimbra 服务器 - Ubuntu - 配置

linux - 更改文件权限 Linux