即使使用 bash 命令(类似于 grep)按字符拆分,如何获得模式匹配?

How to get match of a pattern even if it is splitted by characters using a bash command (similar to grep)?

我正在尝试输出包含特定 word/pattern 的文件的所有行,即使它的字母之间包含其他字符也是如此。

假设我们有一堆域名,我们想过滤掉所有包含“paypal”的域名,我想要这种输出:

pay-pal-secure.com
payppal.net
etc...

我想知道这是否可以通过 grep 实现,或者是否存在其他可以做到的东西。

非常感谢!

paypal 替换为正则表达式 p.*a.*y.*p.*a.*l 以允许字母之间的所有字符。

更新:

使用扩展正则表达式p.{0,2}a.{0,2}y.{0,2}p.{0,2}a.{0,2}l将字母之间的字符限制为none两个。

示例:grep -E 'p.{0,2}a.{0,2}y.{0,2}p.{0,2}a.{0,2}l' file

参见:The Stack Overflow Regular Expressions FAQ

或者您可以使用 agrep(近似 grep):

$ agrep -By paypal file
agrep: 2 words match within 1 error
pay-pal-secure.com
payppal.net