I just want to know how to grep a line which consists of for example pid with number 2. I want to grep the whole line. Also, it is very important to filter only and exactly "2". Because at the moment It filters all the number which have 2 in it.
我只是想知道如何grep一个由例如pid编号为2的行。我想要整行。此外,仅过滤和精确“2”非常重要。因为此刻它会过滤掉其中包含2的所有数字。
2 个解决方案
#1
3
If you want to get the listing for just one particular PID, the -p
option is the best way.
如果要获取仅一个特定PID的列表,则-p选项是最佳方法。
ps -f -p 2
for example
If you want grep
to match a string only if it is the whole word, not part of another word, look at the -w
flag, which matches words like
如果你希望grep只匹配一个字符串,只要它是整个单词,而不是另一个单词的一部分,请查看-w标志,它匹配像
$ echo '52' | grep 2
52
$ echo '52' | grep -w 2
$
if you want to match against only a particular field, awk
might be a better answer than grep
. For example, if we want to check if the second column is exactly 2 we could do
如果你想只匹配一个特定的字段,awk可能是一个比grep更好的答案。例如,如果我们想检查第二列是否正好是2,我们可以做
ps -af | awk '$2 == 2 {print}'
#2
0
You could go for something like this. If you need the details of a process and you know the pid
go for this.
你可以去做这样的事情。如果您需要流程的详细信息,并且您知道pid就可以了。
ps afux | awk '{if($2==<pid>) print}'
#1
3
If you want to get the listing for just one particular PID, the -p
option is the best way.
如果要获取仅一个特定PID的列表,则-p选项是最佳方法。
ps -f -p 2
for example
If you want grep
to match a string only if it is the whole word, not part of another word, look at the -w
flag, which matches words like
如果你希望grep只匹配一个字符串,只要它是整个单词,而不是另一个单词的一部分,请查看-w标志,它匹配像
$ echo '52' | grep 2
52
$ echo '52' | grep -w 2
$
if you want to match against only a particular field, awk
might be a better answer than grep
. For example, if we want to check if the second column is exactly 2 we could do
如果你想只匹配一个特定的字段,awk可能是一个比grep更好的答案。例如,如果我们想检查第二列是否正好是2,我们可以做
ps -af | awk '$2 == 2 {print}'
#2
0
You could go for something like this. If you need the details of a process and you know the pid
go for this.
你可以去做这样的事情。如果您需要流程的详细信息,并且您知道pid就可以了。
ps afux | awk '{if($2==<pid>) print}'