Need help parsing the output of `top -b` stored in a file

35 views Asked by At

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
1

There are 1 answers

0
Adrian Frühwirth On

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:

$ top -b -n 1 | awk -v cpu=5 '$1=="PID"{t=1} t && $9>cpu'
31502 foo    25   5 38984 9292 2168 R   12  0.0   0:00.06 perl

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 dstat instead to collect this information in the future.