bash - grep - 列出不包含两种模式的所有行

标签 bash grep

我有一个包含一些记录的文本文件。我有两个模式要验证,我想列出文件中不包含这两个模式的所有行。如何使用 grep 命令执行此操作?
我使用 grep -v 尝试了一些东西,但似乎没有任何效果。

假设我的文本文件如下。
1.qwerpattern1yui
2. 广告pattern2asd
3. cczxczc
4.jkjkpattern2adsdapattern1

我只想列出第 1、2 和 3 行。

提前致谢。

最佳答案

您可以使用:

grep -w -v -e "word1" -e "word2" file

或者使用 egrep:

egrep -w -v -e "word1|word2" file

更新:根据评论,似乎遵循 awk 会更好:

awk '!(/pattern1/ && /pattern2/)' file

关于bash - grep - 列出不包含两种模式的所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21240337/

相关文章:

linux - 以正确的时间延迟重播 bash session

linux - 在 bash 脚本中运行 tar 返回 "Output with bankrupt according to previous errors"

linux - 用于从文本日志文件中 Grepping/检索值的 Shell 脚本(sqlcode 字段值)

linux - grep Linux 中特定的一段命令输出

linux - 如何为这种情况编写 Grep 命令?

regex - 使用 Grep 查找两个短语(包括短语)之间的文本 block

powershell - 如何在PowerShell中使用Get-Content和Select-String输出与 'this_string'不匹配的行?

bash coproc 和剩余的 coproc 输出

linux - Bash 命令拒绝在后台运行 &

mysql - mysql 数据库克隆的有效方法/工具