How To Capture Unix 'Top' Command Output to a CSV file?
<p>I am trying to get first 5 lines of <code>top</code> command through shell script &amp; I need to write the output to a <strong>csv</strong> file ( I need to monitor the result in every 15 seconds ). Finally I need to plot a graph using the obtained datasheet.</p>

<p>I got the shell scrip to write the first 5 lines of <code>top</code> command to a <strong>txt</strong> file : </p>

<pre><code>#!/bin/bash
echo "execution started.."

top -b -n 3 | sed -n '7,1p' &gt;&gt; out.txt

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

<p>Here is the <strong>out.txt</strong> file after few execution : </p>

<pre><code>    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
</code></pre>

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

<p>Can you please provide a solution to write the same output into a <strong>csv</strong> file ? </p>