我听说 grep 有环视支持。我试图在答案中排除带有 grep 的行,因为显然它通常会在那里。我只是想检查我的 mysqld 服务器是否基本启动。
我正在尝试的正则表达式:
^(?!.*grep)(?=.*mysql).*
Run Code Online (Sandbox Code Playgroud)
命令行。我试图匹配的顶线,我试图排除的底线。
root:~# ps aux | grep mysqld_safe
root 28012 0.0 0.1 4408 712 pts/0 S 18:00 0:00 /bin/sh /usr/bin/mysqld_safe
root 29167 0.0 0.1 9392 900 pts/1 S+ 20:51 0:00 grep --color=auto mysqld_safe
Run Code Online (Sandbox Code Playgroud)
这是我的查询:
# ps aux | grep '^(?!.*grep)(?=.*mysql).*'
(nothing)
Run Code Online (Sandbox Code Playgroud)
要使用前瞻/后瞻,您需要-P向 grep 添加选项(如果您的 grep 支持)。因为 PCRE 支持它们。
ps当您 grep输出但想要排除进程本身时,有一个常见的技巧grep,例如:
ps -ef|grep [m]ysql
Run Code Online (Sandbox Code Playgroud)或者
ps aux|grep [m]ysql
Run Code Online (Sandbox Code Playgroud)