10

I'm relatively new to shell programming and would like to know if there is a simple way to output the results of the sar command to a csv file. Using sar > file1.csv does the job, but it is not properly formatted. All the data is present in one column. I tried this, but it was worse

sar -d -u -w 1 1 | grep -v Average | grep -v Linux | tr -s ' ' ',' | tr -d '\n' > file1.csv

Can anyone give me the right script to store the output of the sar command in a csv file. Help will be appreciated.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
Agi
  • 235
  • 1
  • 2
  • 13

2 Answers2

54

I know this is kind of old but you should, or could, use sadf -dh -- <sar command>. It is part of the sysstat package and it will give you the csv output without any need for awk and regex. Actually, the latest versions are also able to output the info to JSON and XML. You can just pick your poison :)

Simple example:

$ sadf -dh -- -p
localhost.localdomain;-1;2014-06-13 08:47:02 UTC;LINUX-RESTART
# hostname;interval;timestamp;CPU;%user;%nice;%system;%iowait;%steal;%idle[...]
localhost.localdomain;600;2014-06-13 09:00:01 UTC;-1;8.80;0.01;1.65;9.51;0.00;80.03
localhost.localdomain;600;2014-06-13 09:10:01 UTC;-1;3.03;0.71;2.41;0.81;0.00;93.05
PhilR
  • 5,375
  • 1
  • 21
  • 27
mimsugara
  • 839
  • 11
  • 18
  • 4
    If it is a multi cpu-core system omitting h option in sadf will print one line per each cpu-core. This file will be easier to process. – Indika K Jul 06 '15 at 10:29
  • 4
    This answer is heavily underrated. – Alexander Janssen Oct 24 '15 at 06:35
  • **This answer should be marked as the solution**. It do not only gives an answer following the how the question was framed (pipe grep, awk, etc. for a long and complicated one-liner), but it advises the right tool for the job keeping the final solution simpler and more powerful! – kral2 Sep 29 '21 at 13:53
3
sar -d -u -w 1 1 | grep -v Average | grep -v Linux |awk '{if ($0 ~ /[0-9]/) { print $1","$2","$4","$5","$6; }  }'



22:14:04,CPU,%nice,%system,%iowait
22:14:05,all,0.00,8.53,0.00
22:14:04,proc/s,,,
22:14:05,0.00,,,
22:14:04,DEV,rd_sec/s,wr_sec/s,avgrq-sz
22:14:05,dev8-0,0.00,0.00,0.00

outputs above enjoy

V H
  • 8,382
  • 2
  • 28
  • 48
  • 1
    You could also set OFS="," in a BEGIN block. – Kyle Maxwell Mar 12 '13 at 00:46
  • Thanks. Can the same be extended to the top command?. I tried this but I cant see the col names. top -n1 -b |awk '{if ($0 ~ /[0-9]/) { print $1","$2","$4","$5","$6","$7","$8","$9","$10","$11","$12","$13","$14","$15; } }' > file2000.csv – Agi Mar 12 '13 at 15:20
  • 1
    sure, its because it is capturing 0-9 so you could either strip that off which will list all of the top result top -n1 -b |awk '{ print $1","$2","$4","$5","$6","$7","$8","$9","$10","$11","$12","$13","$14","$15; }' or ... top -n1 -b |awk '{if (($1 ~ /^[0-9]/) || ($0 ~ /PID/ )) { print $1","$2","$4","$5","$6","$7","$8","$9","$10","$11","$12","$13","$14","$15; } }' which will list PID line and all of the top result – V H Mar 12 '13 at 16:52