Shell

使用 ag 搜尋有無模式

  • January 17, 2018

有兩個文件

echo 'example.com/call_me?param' > file1.txt
echo 'example.com/call_me_maybe?param' >> file1.txt
echo 'example.com/call_me?' >> file1.txt
echo 'example.com/call_me?param' > file2.txt
echo 'example.com/call_me_maybe?' >> file2.txt

$ ag -v '\?param'
file2.txt
2:example.com/call_me_maybe?

file1.txt
3:example.com/call_me?

$ ag '\/call_me_maybe' -v '\?param'
ERR: Error stat()ing: \?param
ERR: Error opening directory \?param: No such file or directory

$ ag '\/call_me_maybe' -v '\?param' file1.txt
ERR: Error stat()ing: \?param
ERR: Error opening directory \?param: No such file or directory
file1.txt
1:example.com/call_me?param
3:example.com/call_me?

我想搜尋一個術語並排除另一種模式。是否可以在一個命令中執行?

我使用 ag 是因為它會探勘log.gz文件。所以我可以使用zgrepor ag -z

我不使用ag,但根據手冊頁,命令行是:

ag [options] pattern [path ...]

這意味著只允許一種模式。

就像grep您想將搜尋與反向搜尋結合起來(即執行搜尋a AND NOT b)一樣,您可以將輸出通過管道傳輸到grep -v .... 例如

ag '\/call_me_maybe' | grep -v '\?param'

引用自:https://unix.stackexchange.com/questions/417789