top in batch mode, cpu info is wrong

well. the title says it all.
im runing top in batch mode like this

top -b -n1 > somefile

but the cpu usage info is not correct.
if i run top normally, the first second, i see the same wrong info, and then it corrects itself.
i found only one small mention of it on this forum. with this link
top(1): tasks - Linux man page
in the bugs section, it says to use a env variable called CPULOOP, but my top is not paying attention

the system is gentoo

cat /etc/gentoo-release 
Gentoo Base System release 1.12.11.1
top -V
top: procps version 3.2.7

if i run 2 iterations of top like this

top -b n2 > somefile 

then the info is ok the second time, but the "somefile" file gets 2 times the output, so, in my script, when i sort it and search for it, the info is duplicated !!!

so any help on how to fix this would be greatly apreciated
pd: the that uses top is the one been discused here

If the second iteration is correct you can shorten the delay with the -d option and print the result of the second iteration with awk, this works on a Debian Linux system:

top -b -n2 -d0.01 | awk '/^top/{i++}i==2' > somefile

Regards

thanks, but it didnt worked :confused:

this is a screenshot of my script (below) and top at the same time.
screenshot on Flickr - Photo Sharing!

I see no script in that screen shot.

In what way did Franklin52's solution not work?

sorry, that was my mistake.
the screenshot is of my script runing.
between the terminal and the fluxbox panel, you can see 2 lines of colored text.

that is dzen.
the first number after CPU is the iddle percentage.

and what frankling said didnt worked because the problem persisted.

if i run

while : 
do
   top -b -n1 > file
   cat file
done

and top, at the same time, looking at both at the same time
the one in the while loop show an iddle percentage of 56, when top in normal mode (and any other app to check cpu usage) show the pc in 96 iddle time

How about without the shortened delay:

top -b -n2 | awk '/^top/{i++}i==2' > somefile

im having trouble understanding that awk sentence .
it searchs for a line begening with "top", and when is found, increment i,and if i equals to 2 .....

if you are trying to use awk to filter the output to get the same result as one run, then why....., shouldnt there be a print somewhere ?

similar to this?

top -b -n2 | awk ' /^top/  { i++ } \
                          i == 2 {print }' > somefile

that makes me wonder, if tops output is always the asme lenght ...
i could use something like NR >= $(top height)

keep in mind that im trying to fit all this in one awk, but each day it gets more complicated

Your output has 2 sections beginning with the word top and we want to print the second one.
After we met the second top i has the value 2.
i==2 is a condition here for awk, if it's true awk print the record per default so awk begins to print after we've reach the second top.
i==2 is similar to {if i == 2 {print $0}}

Have a read of an awk tutorial, you can find some links here:

http://www.unix.com/answers-frequently-asked-questions/13774-unix-tutorials-programming-tutorials-shell-scripting-tutorials.html

Hope this helps.