bash 如何在 Mac OS X 中删除 ps 命令中的标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11532188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:47:54  来源:igfitidea点击:

How to get rid of the headers in a ps command in Mac OS X ?

macosbashshellps

提问by Pradep

I use a specific ps command namely

我使用特定的 ps 命令

ps -p <pid> -o %cpu, %mem

which gives me a result like

这给了我一个结果

 %CPU %MEM
 15.1 10.0

All i want to do is to just print these numbers like 15.1 and 10.0 without the headers. I tried to use the 'cut' . But it seems to work on every line.

我想要做的就是只打印这些数字,如 15.1 和 10.0 而不带标题。我尝试使用 'cut' 。但它似乎适用于每一行。

i.e

IE

echo "$(ps -p 747 -o %cpu,%mem)" | cut -c 1-5

gives something like

给出类似的东西

 %CPU
  8.0

How to get just the numbers without the headers ?

如何只获取没有标题的数字?

采纳答案by Steve

Using awk:

使用awk

ps -p 747 -o %cpu,%mem | awk 'NR>1'

Using sed:

使用sed

ps -p 747 -o %cpu,%mem | sed 1d

回答by abarnert

The BSD (and more generally POSIX) equivalent of GNU's ps --no-headersis a bit annoying, but, from the man page:

GNU 的 BSD(以及更普遍的 POSIX)等价物ps --no-headers有点烦人,但是,从手册页:

 -o      Display information associated with the space or comma sepa-
         rated list of keywords specified.  Multiple keywords may also
         be given in the form of more than one -o option.  Keywords may
         be appended with an equals (`=') sign and a string.  This
         causes the printed header to use the specified string instead
         of the standard header.  If all keywords have empty header
         texts, no header line is written.
 -o      Display information associated with the space or comma sepa-
         rated list of keywords specified.  Multiple keywords may also
         be given in the form of more than one -o option.  Keywords may
         be appended with an equals (`=') sign and a string.  This
         causes the printed header to use the specified string instead
         of the standard header.  If all keywords have empty header
         texts, no header line is written.

So:

所以:

ps -p 747 -o '%cpu=,%mem='

That's it.

就是这样。

If you ever do need the remove the first line from an arbitrary command, tail makes that easy:

如果您确实需要从任意命令中删除第一行,tail 可以轻松实现:

ps -p 747 -o '%cpu,%mem' | tail +2

Or, if you want to be completely portable:

或者,如果你想完全便携:

ps -p 747 -o '%cpu,%mem' | tail -n +2

The cutcommand is sort of the column-based equivalent of the simpler row-based commands headand tail. (If you really do want to cut columns, it works… but in this case, you probably don't; it's much simpler to pass the -oparams you want to ps in the first place, than to pass extras and try to snip them out.)

cut命令类似于更简单的基于行的命令的基于列的等效项headtail. (如果你真的想剪切列,它可以工作......但在这种情况下,你可能不会;首先传递-o你想要 ps的参数比传递额外的并尝试将它们剪掉要简单得多.)

Meanwhile, I'm not sure why you think you need to eval something as the argument to echo, when that has the same effect as running it directly, and just makes things more complicated. For example, the following two lines are equivalent:

同时,我不知道为什么你认为你需要评估一些东西作为 echo 的参数,当它与直接运行它具有相同的效果时,只会让事情变得更加复杂。例如,以下两行是等效的:

echo "$(ps -p 747 -o %cpu,%mem)" | cut -c 1-5
ps -p 747 -o %cpu,%mem | cut -c 1-5

回答by mike

Use ps --no-headers:

使用ps --no-headers

--no-headers print no header line at all

--no-headers print no header line at all

or use:

或使用:

ps | tail -n +2

回答by David W.

Already picked the winner. Drats...

已经选出了赢家。德拉特...

If you're already using the -oparameter, you can specify the headings for the particular columns you want to print by putting an equal sign after the name, and the column name. If you put a null string, it'll print no headings:

如果您已经在使用该-o参数,则可以通过在名称和列名称后面放置一个等号来指定要打印的特定列的标题。如果你输入一个空字符串,它不会打印任何标题:

With standard headings (as you had):

使用标准标题(如您所见):

$ ps -p $pid -o%cpu,%mem
 %CPU %MEM
  0.0  0.0

With custom headings (just to show you how it works):

使用自定义标题(只是为了向您展示它是如何工作的):

$  ps -p $pid -o%cpu=FOO,%mem=BAR
  FOO  BAR
  0.0  0.0

With null headings (Notice it doesn't even print a blank line):

使用空标题(注意它甚至不打印空行):

$ ps -p $pid -o%cpu="",%mem=""
 0.0   0.0