仅当多个模式匹配时 Grep 排除行
Grep exclude line only if multiple patterns match
我想查找不包含 "path": "/"
和 "User-Agent": "curl
的行。应包含仅包含其中一个的行。
换句话说,如何排除匹配多个模式的行?
你可以使用 |在 grep 命令中发出信号 "or",您可以使用 -v 参数
所以:
grep -v \"path\"\:\ \"/\"\\|curl
将打印不包含 "path": "/" 或 curl 的每一行。
在同一行中进行这些双重检查最好使用 awk
:
awk '! (/"path": "\/"/ && /"User-Agent": "curl/)' file
这使用逻辑 awk '! (condition1 && condition2)'
,因此只要找到两个字符串,它就会失败。
测试
$ cat a
"path": "/" and "User-Agent": "curl
"path": "/" hello
bye "User-Agent": "curl
this is a test
$ awk '! (/"path": "\/"/ && /"User-Agent": "curl/)' a
"path": "/" hello
bye "User-Agent": "curl
this is a test
这个 awk 应该可以完成这项工作:
awk '(/"path": "\/"/ && !/"User-Agent": "curl/) ||
(!/"path": "\/"/ && /"User-Agent": "curl/)
{print FILENAME ":" [=10=]}' *
这是通过这种方法进行的:
awk '(/match1/ && !/match2/) || (!/match1/ && /match2/)' *
换句话说,如果两个词中的任何一个匹配则打印,但两个都匹配时则不打印。
我想查找不包含 "path": "/"
和 "User-Agent": "curl
的行。应包含仅包含其中一个的行。
换句话说,如何排除匹配多个模式的行?
你可以使用 |在 grep 命令中发出信号 "or",您可以使用 -v 参数
所以:
grep -v \"path\"\:\ \"/\"\\|curl
将打印不包含 "path": "/" 或 curl 的每一行。
在同一行中进行这些双重检查最好使用 awk
:
awk '! (/"path": "\/"/ && /"User-Agent": "curl/)' file
这使用逻辑 awk '! (condition1 && condition2)'
,因此只要找到两个字符串,它就会失败。
测试
$ cat a
"path": "/" and "User-Agent": "curl
"path": "/" hello
bye "User-Agent": "curl
this is a test
$ awk '! (/"path": "\/"/ && /"User-Agent": "curl/)' a
"path": "/" hello
bye "User-Agent": "curl
this is a test
这个 awk 应该可以完成这项工作:
awk '(/"path": "\/"/ && !/"User-Agent": "curl/) ||
(!/"path": "\/"/ && /"User-Agent": "curl/)
{print FILENAME ":" [=10=]}' *
这是通过这种方法进行的:
awk '(/match1/ && !/match2/) || (!/match1/ && /match2/)' *
换句话说,如果两个词中的任何一个匹配则打印,但两个都匹配时则不打印。