Search code examples
shellunixsar

Output sar command results to a csv file


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.


Solution

  • 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