How To Capture Unix 'Top' Command Output t

2019-07-21 02:30发布

问题:

I am trying to get first 5 lines of top command through shell script & I need to write the output to a csv file ( I need to monitor the result in every 15 seconds ). Finally I need to plot a graph using the obtained datasheet.

I got the shell scrip to write the first 5 lines of top command to a txt file :

#!/bin/bash
echo "execution started.."

top -b -n 3 | sed -n '7,1p' >> out.txt

while [ true ]; do
    sleep 15
    echo "running.."
    top -b -n 3 | sed -n '8, 12p' >> out.txt
done

Here is the out.txt file after few execution :

    PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 3983 arun      20   0 1662480 309580  40936 S 26.3  6.4  13:36.00 gnome-shell
17907 arun      20   0  130020   1680   1172 R 10.5  0.0   0:00.03 top
 2016 root      20   0  221792  51172   9636 S  5.3  1.1   4:40.97 Xorg
11917 arun      20   0 7004884 570312  22040 S  5.3 11.7   0:48.83 java
    1 root      20   0   59732   7156   3992 S  0.0  0.1   0:02.71 systemd
 3983 arun      20   0 1662480 309580  40936 S 36.8  6.4  13:37.23 gnome-shell
 2016 root      20   0  221792  51172   9636 S 10.5  1.1   4:41.14 Xorg
 2720 mongod    20   0  624364  33716   5200 R  5.3  0.7   1:44.36 mongod
17918 arun      20   0  130020   1676   1172 R  5.3  0.0   0:00.02 top
    1 root      20   0   59732   7156   3992 S  0.0  0.1   0:02.71 systemd
 3983 arun      20   0 1662480 309580  40936 S 25.0  6.4  13:38.60 gnome-shell
 2720 mongod    20   0  624364  33672   5160 S  5.0  0.7   1:44.46 mongod
12081 arun      20   0 2687496 314248  21436 S  5.0  6.5   3:05.51 java
17922 arun      20   0  130020   1680   1172 R  5.0  0.0   0:00.02 top
    1 root      20   0   59732   7156   3992 S  0.0  0.1   0:02.71 systemd

But I need the same data in csv format. I have tried to do this by giving output file name as out.csv ! But that didn't work. ( Because it was not in proper format, whole data came within first shell ! )

Can you please provide a solution to write the same output into a csv file ?

回答1:

If you want to trim runs of whitespace and replace them with commas, try

top -b -n 3 | sed -n '8, 12{s/^ *//;s/ *$//;s/  */,/gp;};12q'

The sed script performs the following substitutions on lines 8 through 12:

  • Replace any leading space with nothing (otherwise you get an empty first column when the PID is right-aligned).
  • Replace any trailing spaces with nothing (similarly to avoid empty fields after the data).
  • Replace any remaining runs of adjacent spaces with a comma. Print this line.

Finally, on line 12, we are done, so we quit sed.

The shell does not pay any attention to the name of the file you are redirecting into and generally, file extensions on Unix are informal decorations, not file type specifiers like they are on some platforms.

You could do echo hello >outputfile.ps and the output would still be text, not PostScript (or a number of other possible interpretations of the .ps file extension). In any event, the echo command does not know that it is being redirected, because that is handled by the shell before the command runs, anyway. (Well, echo is a shell built-in, so in theory there could be some coordination in this case.)