grep带有特殊字符的特定文本

时间:2019-05-28 10:14:44

标签: shell grep

我正在尝试在日志文件中grep特定文本,但是我无法获得我感兴趣的特定文本

echo "[27 May 2019 23:42:32,418] [Accept-Nsh-Proxy-Thread] [ERROR] [::] [Appserver] Too many open files (Accept failed)" >> sl3737474646464-config_console.log | tail -f sl3737474646464-config_console.log
cat sl3737474646464-config_console.log | grep -i "\[Appserver\] Too many open files \(Accept failed\)" 

但无法获取字符串

如何获得我回显的文字?

1 个答案:

答案 0 :(得分:0)

grep\(作为捕获组的开始。只需将\(中的)放下即可。

$ echo "[27 May 2019 23:42:32,418] [Accept-Nsh-Proxy-Thread] [ERROR] [::] [Appserver] Too many open files (Accept failed)" | grep -i "\[Appserver\] Too many open files (Accept failed)"
[27 May 2019 23:42:32,418] [Accept-Nsh-Proxy-Thread] [ERROR] [::] [Appserver] Too many open files (Accept failed)

或使用grep -F开关。这会使grep从字面上解析该参数,因此您可以忘记转义:

$ echo "[27 May 2019 23:42:32,418] [Accept-Nsh-Proxy-Thread] [ERROR] [::] [Appserver] Too many open files (Accept failed)" | grep -i -F "[Appserver] Too many open files (Accept failed)"
[27 May 2019 23:42:32,418] [Accept-Nsh-Proxy-Thread] [ERROR] [::] [Appserver] Too many open files (Accept failed)