The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
redirected output not going to file for all cases brdholman UNIX for Dummies Questions & Answers 7 10-10-2007 11:07 AM
grep data and add to file nbananda Shell Programming and Scripting 5 09-25-2006 07:21 AM
Pipe Data From Grep Into A File katinicsdad Shell Programming and Scripting 4 09-08-2006 08:20 AM
search for a file - errors redirected matrixmadhan AIX 1 07-18-2005 01:12 AM
Saving a redirected file JSP UNIX for Dummies Questions & Answers 1 09-27-2001 03:52 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-23-2008
Registered User
 

Join Date: Nov 2006
Posts: 54
no data redirected to a file with top and grep - why?

HI all,

I want to capture cpu data in batch mode of "top" command and redirect to a file like this:
Code:
top -b > cpu.dat
it works!
But I want to capture only Cpu lines, so i have:
Code:
top -b | grep ^Cpu >cpu.dat
Then I got an empty output file.
Why?
Could somebody explain and help me to make it work.
Thanks
Reply With Quote
Forum Sponsor
  #2  
Old 04-23-2008
Registered User
 

Join Date: Sep 2005
Location: Slovenija
Posts: 20
Hard to explain if we do not know input data.
Anyway - 'grep ^Cpu' should work. Empty output file tells only that there are no lines with word "Cpu" on the beggining of a line.
Reply With Quote
  #3  
Old 04-23-2008
Registered User
 

Join Date: Dec 2005
Posts: 74
Thumbs up

hi,
the direct answer for your 'why' is
->the output is not going to std out it is going to std err


secondly how to accomplish it
->method 1
$top 2>outputOfTop
or
$top >& outputOfTop

then
$grep pattern outputOfTop

->method 2
open two terminal term1 and term2

in term1
$mkfifo pipe
$top 2>pipe

in term2
$grep pattern pipe

make sure the working directory is same in both the terminal
i hope this should work

Best Regards,
Rakesh UV
Reply With Quote
  #4  
Old 04-24-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
uvrakesh: if "top -b >cpu.dat" worked then the output was not being written to stderr.

My copy of top (Ubuntu Linux 7.10) writes to stdout. But it keeps on looping even with the -b option, so I don't have exactly the same version.

fongthai: if top -b >cpu.dat works then does grep ^Cpu cpu.dat work? That would be a workaround at least.
Reply With Quote
  #5  
Old 04-24-2008
Registered User
 

Join Date: Nov 2006
Posts: 54
top -b | grep ^Cpu output is sure no error, it output should be like this:

"Cpu(s): 1.7%us, 0.8%sy, 0.0%ni, 96.9%id, 0.5%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu(s): 0.0%us, 1.3%sy, 0.0%ni, 98.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st"

i should go to stdout. I think.

I tried your methods but they don't work.
Any other idea?
Reply With Quote
  #6  
Old 04-24-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Repeat: do you get these lines in the file when you do top -b >cpu.dat?
Reply With Quote
  #7  
Old 04-24-2008
Registered User
 

Join Date: Nov 2006
Posts: 54
Yes, I got them.
I don't want to get data first then using grep, because it make output file become a huge file. I want to run top -b for one or two days, that's the issue.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:21 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0