How To Capture Unix 'Top' Command Output to a CSV file?

Active3 hr before
Viewed126 times

9 Answers


Replace any trailing spaces with nothing (similarly to avoid empty fields after the data).,Replace any leading space with nothing (otherwise you get an empty first column when the PID is right-aligned).,Replace any remaining runs of adjacent spaces with a comma. Print this line.,If you want to trim runs of whitespace and replace them with commas, try

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'

Redirect loop output to file.,The 10 Top GUI Tools for Linux System Administrators,Linux top command is highly used by system administrators to display system statistics in real time regarding system uptime and load average, used memory, running tasks, a summary of processes or threads and detailed information about each running process.,These are just a few examples on how you can monitor and gather system and process statistics via top command.

In the below example, we’ll redirect the output of top command to top.txt file in the current working directory. The -n argument will be used to send only one snapshot of the command to the mentioned file.

$ top - b - n 1 > top.txt
load more v

Answer: Use the top command batch mode operation option ( -b ) to capture the top command output into a file.,To avoid this problem and get a readable top command output, use option -b in the top command. Execute top command in batch mode as shown below.,Question: I’m trying to capture the output of the top command into a file. When I execute top > output.txt, the output.txt file contains lot of junk characters. What is the best method to capture the output of the top command into a readable text file?,If you try to redirect the top command output to a text file as shown below, you’ll notice that the output file contains lot of junk characters.

When you try to view the output file using less command, you’ll notice that the output file is created with lot of junk characters.

$ top - n 1 > top - output.txt

$ less top - output.txt "top-output.txt"
may be a binary file.See it anyway ?
load more v

You will see that in the first example the time is defined by top's update interval ( set to 10 seconds via -d 10), while in the second the sleep command in the while loop is the limit. Note that also top never finishes in the first example, which makes processing any piped output a problem, as the program that reads from stin never gets a "process ended" signal which especially is a problem for column-based programs like awk or cut. See how e.g.,will not produce output at the intervals top produces it, but rather in irregular batches, while, Why is the output always provide the same percentage no matter the current load or stress test? – linguru772x Aug 16 '15 at 2:47 , 2 If you're not interested in per-process statistics, you may want to consider the CPU fields of vmstat or mpstat (which can be polled at specified intervals without resorting to an explicit shell loop e.g. vmstat 5 | awk ...) – steeldriver Aug 15 '15 at 22:59

You can do this via vmstat:


open VMSTAT, "vmstat 5|";
<VMSTAT>; <VMSTAT>; # skip the header
while (<VMSTAT>) {
    @now = split;
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime (time);
    printf "%d-%02d-%02d %02d:%02d:%02d,%d%\n", $year+1900, $mon, $mday, $hour, $min, $sec,
        $now[12] + $now[13];
load more v

df - h | sed 's/ */,/g' > out.csv
load more v

Woow this is fantastic too. Now just redirect the output to a file: ,Well, if you capture top each 3 seconds you can do this: ,This command will show something like the following: ,The output is below:

top - b - d 1 > top.txt
load more v

As has already been said the option -b together with -n 1 will give a single text output from top rather than the constantly updating default. Then you can pipe to sed to find the value you are looking for., Stack Exchange network consists of 178 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. ,Press ESC to go back to the main Top menu and save with Capital W the change. Now you command is easier to parse.,Regarding to your comment: If you redirect standard output to a file with top -b -d 1 > file.csv, you cannot pipe standard output into the grep command.

Try with a small number of iterations first (note: -n2 means 2 iterations):

top - b - d1 - n2 | grep Cpu | cut - c 35 - 39


echo "Standard output" | grep "out"


echo "Standard output" > /tmp/foo | grep "out"
load more v

You only need to replace the tab with comas and set the output to a csv file:,the text in grep can be a cat to your file to get all the content, Hi zeel, This command will grab out the only columns that you need from the "last" command and will pour it into the excel file. – Dhimant Thanki Aug 11 '18 at 20:25 ,The final format, I would like to achieve, right now I have to import the data manually from a txt file is the following:

You only need to replace the tab with comas and set the output to a csv file:

grep "hello    world" | tr "\\t"
"," > file.csv

I'm not sure if it is a tab or only a white space on your output, in case there is a white space:

grep "hello world" | tr " "
"," > file.csv
load more v

TOP is the first command that came to mind whenever someone asks about system resource utilization monitoring. CPU, memory utilization can be easily tracked in this tool. This tool is available to almost every UNIX and Linux flavor.,Certified Kubernetes Administrator,File encryption / password protect file in HPUX,12 examples of ls command in Linux for daily use

# top - n 1 - b > /tmp/top_output.txt
# cat / tmp / top_output.txt
top - 02: 58: 22 up 278 days, 7: 19, 1 user, load average: 0.56, 0.52, 0.56
Tasks: 189 total, 1 running, 188 sleeping, 0 stopped, 0 zombie
Cpu(s): 10.8 % us, 4.9 % sy, 0.0 % ni, 84.0 % id, 0.2 % wa, 0.0 % hi, 0.0 % si, 0.0 % st
Mem: 12199956 k total, 7774572 k used, 4425384 k free, 856212 k buffers
Swap: 8388604 k total, 0 k used, 8388604 k free, 4787268 k cached

2994 root RT 0 688 m 84 m 55 m S 7.9 0.7 2998: 17 osysmond.bin
4227 grid 20 0 501 m 29 m 17 m S 2.0 0.3 458: 12.36 oracle
4229 grid - 2 0 500 m 29 m 17 m S 2.0 0.3 1009: 43 oracle
4251 grid 20 0 485 m 27 m 25 m S 2.0 0.2 112: 07.76 oracle
4266 root 20 0 2206 m 73 m 25 m S 2.0 0.6 2519: 45 crsd.bin
1 root 20 0 19400 1552 1228 S 0.0 0.0 0: 03.23 init
2 root 20 0 0 0 0 S 0.0 0.0 0: 00.41 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 56: 25.42 ksoftirqd / 0
   -- -- - output clipped-- -- -
load more v

Other "capture-undefined" queries related to "How To Capture Unix 'Top' Command Output to a CSV file?"