如何列出相似的行?

如何列出相似的行?

分隔符可以是:“”; “_”

输入:

foo#foo_ehh113#The password of user 111 will expire within the next seven d
foo#foo_ehh204#The password of user 111 will expire within the next seven d
sadf#sadf this is a sample_text
foo#foo_ehh204#The password of user 222 will expire within the next seven da
foo - moreeee test
foo#foo_ehh113#The password of user 222 will expire within the next seven da
foo#foo_ehh113#The password of user 333 will expire within the next seven day
ldr#ldr_another sample text
foo#foo_ehh204#The password of user 333 will expire within the next seven day

输出:

foo#foo_ehh113#The password of user 111 will expire within the next seven d
foo#foo_ehh204#The password of user 111 will expire within the next seven d
foo#foo_ehh204#The password of user 222 will expire within the next seven da
foo#foo_ehh113#The password of user 222 will expire within the next seven da
foo#foo_ehh113#The password of user 333 will expire within the next seven day
foo#foo_ehh204#The password of user 333 will expire within the next seven day

因此给定行中的类似单词例如:

The
password
of
user
will
expire
within
the

我的问题:有没有什么方法可以只输出在一定程度上相似的行?前任。它们匹配 8 个单词
有没有任何 shell 脚本可以检测到这一点?

答案1

如果你想按顺序匹配文本,你可以尝试:

$ grep 'The.*password.*of.*user.*will.*expire.*within.*the' file 
foo#foo_ehh113#The password of user 111 will expire within the next seven d
foo#foo_ehh204#The password of user 111 will expire within the next seven d
foo#foo_ehh204#The password of user 222 will expire within the next seven da
foo#foo_ehh113#The password of user 222 will expire within the next seven da
foo#foo_ehh113#The password of user 333 will expire within the next seven day
foo#foo_ehh204#The password of user 333 will expire within the next seven day

如果您有一个保存文本组的文件(名为 group.txt),您可以使用:

$ grep $(printf "%s.*" $(cat group.txt)) file

答案2

您可以在命令行中输入下一个,您可以根据需要提供任意多个关键字:

grep -E "one|two|three" file.txt

如果源是 dmesg 则执行以下操作:

dmesg | grep -E "one|two|three" 

相关内容