在 bash 命令输出的每一行前面加上时间前缀

在 bash 命令输出的每一行前面加上时间前缀

我正在运行top命令来查看有关特定进程的详细信息。输出通过管道传输, grep如下所示:

top -n 1 | grep jre

输出通常大约有 4 行,我想在每行前面加上当前时间,因此输出将是这样的:

前:

2772 deleteme  20   0  2832 1156  872 R  2.0  0.1   0:00.01 top  

后:

13:46 25-08-2012 2772 deleteme  20   0  2832 1156  872 R  2.0  0.1   0:00.01 top  

答案1

查看ts来自更多工具包裹:

NAME
       ts - timestamp input

SYNOPSIS
       ts [-r] [format]

DESCRIPTION
       ts adds a timestamp to the beginning of each line of input.

例如,您可以这样使用它:

$ top -n 1 | grep init | ts
aug 28 17:15:00     1 root      20   0 24448 2272 1340 S    0  0.1   0:01.07 init

答案2

ps命令更适合此类任务。尝试以下操作:

$ ps -ao bsdstart,fuser,pid,%cpu,%mem,args | grep jre

ps手册页中:

ps 显示有关所选活动进程的信息。如果您希望重复更新所选内容和显示的信息,请使用 top(1)。

在我建议的命令中,选项“-a”指示ps打印所有用户的进程。-o指定输出格式。在我的示例中(同样来自ps手册页):

bsdstart : time the command started.  If the process was
                             started less than 24 hours ago, the output format
                             is " HH:MM", else it is " Mmm:SS" (where Mmm is
                             the three letters of the month).  See also
                             lstart, start, start_time, and stime.

fuser    :  filesystem access user ID.  This will be the
                             textual user ID, if it can be obtained and the
                             field width permits, or a decimal representation
                             otherwise.
pid      :  a number representing the process ID (alias tgid).

%cpu     :  cpu utilization of the process in "##.#" format.
                             Currently, it is the CPU time used divided by the
                             time the process has been running
                             (cputime/realtime ratio), expressed as a
                             percentage.  It will not add up to 100% unless
                             you are lucky.  (alias pcpu).
%mem     :  ratio of the process's resident set size  to the
                             physical memory on the machine, expressed as a
                             percentage.  (alias pmem).

args     : command with all its arguments as a string.
                             Modifications to the arguments may be shown.  The
                             output in this column may contain spaces.  A
                             process marked <defunct> is partly dead, waiting
                             to be fully destroyed by its parent.  Sometimes
                             the process args will be unavailable; when this
                             happens, ps will instead print the executable
                             name in brackets.  (alias cmd, command).  See
                             also the comm format keyword, the -f option, and
                             the c option.
                             When specified last, this column will extend to
                             the edge of the display.  If ps can not determine
                             display width, as when output is redirected
                             (piped) into a file or another command, the
                             output width is undefined (it may be 80,
                             unlimited, determined by the TERM variable, and
                             so on).  The COLUMNS environment variable or
                             --cols option may be used to exactly determine
                             the width in this case.  The w or -w option may
                             be also be used to adjust width.

您可以根据需要进行更改。查看man ps并搜索“标准格式说明符”(您可以在手册页中使用 vi 样式搜索,点击“/”并输入您的搜索模式,“n”将移动到下一个匹配项)。

相关内容