#!/bin/bash
# ps -ely | sort -nrk 8 | head -n 20 | awk 'BEGIN{print "UID", "PID", "PPID", "RSS", "CMD"} {print $2, $3, $4, $8/1024"M", $NF}' | column -t
fmt="%-9s%-9s%-6s%-6s\033[1;32m%-8s\033[0m%-10s\n"
awk -v fmt=$fmt 'BEGIN{printf fmt, "PID","PPID","%CPU","%MEM","RSS","COMM"}' > /tmp/mem.top
ps -eo pid,ppid,pcpu,pmem,rss,comm | grep -Ev "grep|ps" | sort -nrk 5 | head -n 20 | awk -v fmt=$fmt '{printf fmt, $1,$2,$3,$4,int($5/1024)"M",$6}' >> /tmp/mem.top

> /tmp/deli.top
for i in `seq 21`; do
    echo '|' >> /tmp/deli.top
done

fmt="%-9s%-9s\033[1;34m%-6s\033[0m%-6s%-8s%-10s\n"
awk -v fmt=$fmt 'BEGIN{printf fmt, "PID","PPID","%CPU","%MEM","RSS","COMM"}' > /tmp/cpu.top
ps -eo pid,ppid,pcpu,pmem,rss,comm | sort -nrk 3 | grep -Ev "grep|ps|utool" | head -n 20 | awk -v fmt=$fmt '{printf fmt, $1,$2,$3,$4,int($5/1024)"M",$6}' >> /tmp/cpu.top

paste /tmp/mem.top /tmp/deli.top /tmp/cpu.top | column -t
rm -rf /tmp/{mem.top,cpu.top,deli.top}
