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 ?
 
    