I have a file containing the output of top -b. Now I want to get the processes which are utilizing more than 100MB of memory or more than 5% of CPU.
The script which I have written but it's not working properly.
while read line
do
curr_PID=`echo $line | awk '{print $1}'`
curr_USE=`echo $line | awk '{print $2}'`
curr_MEM=`echo $line | awk '{print $6}'`
curr_MEM2=`echo $line | awk '{print $6}' | cut -d '=' -f2 | sed 's/.$//'`
curr_CPU=`echo $line | awk '{print $10}' | cut -d '=' -f2 | sed 's/.$//'`
curr_CMD=`echo $line | awk '{print $11}'`
mem_type=`echo $curr_MEM | awk '{print substr($0,length,1)}'`
if [ "$mem_type" = "K" ]
then
# temp_curr_MEM=$(expr $curr_MEM / 1024)
echo "Hi"
fi
if [ "$curr_CPU" -gt 5 ] || [ "$curr_MEM2" -gt 50 ]
then
echo $line
fi
done < top_output_sorted.tmp
It will be easier, more readable and faster to do the whole processing in
awk. E.g., the CPU part of your question can be achieved using:This can be easily extended to also match against whichever memory column you are interested in, just make sure to account for unit conversion properly.
On a side note, you might want to consider using
dstatinstead to collect this information in the future.