2017-08-14 20 views
2

如果我cat文本文件包含mysqld_safemysqld然後grep -w按預期工作。但是當用PS輸出管道時,它不起作用。Grep確切的字符串管道不工作

ps -ef | grep -w mysqld 

輸出的兩條線。

/usr/bin/mysqld_safe 
/usr/libexec/mysqld 

我期待的只有mysqld。我知道排除選項grep -v mysqld_safe

版本 - grep (GNU grep) 2.5.1

+0

嘛,'grep的「\ bmysqld \ b''不能在'mysqld_safe'中找到'mysqld'。僅僅因爲'_'是一個單詞char,'d'和'_'之間沒有單詞邊界。 –

+0

假設bash,你必須單引號的正則表達式。就目前而言,grep看到的正則表達式2是'bmysqlb',不應該匹配任何這些行。 [「不加引號的反斜槓(\\)是轉義字符,它保留了下一個字符的文字值](https://linux.die.net/man/1/bash) –

+0

這不是作業,我試圖解決監控問題。我想先過濾這個,然後再用json輸出一個軟件。另外\ b是我在幾分鐘前學過的東西,然後在這裏發佈問題。有很多類似的問題,我經歷了其中的大部分,這就是我遇到grep邊界的原因。 – anon

回答

1

如果你有pgrep,使用pgrep -x mysqld將是更好的選擇比ps + grep

man pgrep

pgrep, pkill - look up or signal processes based on name and other attributes 

    -x, --exact 
      Only match processes whose names (or command line if -f is specified) exactly match the pattern. 

    -l, --list-name 
      List the process name as well as the process ID. (pgrep only.) 

    -c, --count 
      Suppress normal output; instead print a count of matching processes. When count does not match any‐ 
      thing, e.g. returns zero, the command will return non-zero value. 

    -n, --newest 
      Select only the newest (most recently started) of the matching processes.