LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Problem in redirecting awk output into a file (http://www.linuxquestions.org/questions/linux-general-1/problem-in-redirecting-awk-output-into-a-file-4175430848/)

paldebojyoti 10-06-2012 11:53 AM

Problem in redirecting awk output into a file
 
Hello,
I am trying to redirect the CPU Usage and Memory Usage of a process into a file. To do this I am using top command with awk:
top -S -d 1 | awk '/'"gnome-terminal"'/ {print $10,":",$11,";" > "/tmp/test.log";}'
The generated log file contains many junk data along with the following wrong data:

%CPU : %MEM ;
2 : 0.0 ;
2 : 2.1 ;
0.0 : 0:00.02 ;
0.9 : 0:37.80 ;
0 : 0.0 ;
0 : 0.0 ;
0 : 0.0 ;
0 : 0.0 ;
0 : 0.0 ;
0 : 0.0 ;
0 : 0.0 ;
0 : 0.0 ;
0 : 0.0 ;
0 : 0.0 ;
0 : 0.0 ;

But when I am executing the following command:
top -S -d 1 | awk '/'"gnome-terminal"'/ {print $10,":",$11,";";}'
It is giving correct output:
1 : 0.4 ;
1 : 0.4 ;
1 : 0.4 ;
1 : 0.4 ;
1 : 0.4 ;
1 : 0.4 ;
1 : 0.4 ;
3 : 0.4 ;
2 : 0.4 ;
2 : 0.4 ;

Is there any way to fix this problem?

Thanks in advance.
Regards,
Deb

nugat 10-06-2012 05:34 PM

maybe try putting your redirection to file after the closing awk curly brace, e.g.:
Code:

top -n1 -S -d 1 | awk '/gnome-terminal/ {print $10,":",$11,";"}' > /tmp/test.log

Habitual 10-06-2012 06:19 PM

Quote:

Originally Posted by nugat (Post 4799003)
Code:

top -n1 -S -d 1 | awk '/gnome-terminal/ {print $10,":",$11,";"}' > /tmp/test.log

Good eye!


All times are GMT -5. The time now is 04:27 AM.