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 and shell scripting languages 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 03:07 PM
grep data and add to file nbananda Shell Programming and Scripting 5 09-25-2006 11:21 AM
Pipe Data From Grep Into A File katinicsdad Shell Programming and Scripting 4 09-08-2006 12:20 PM
search for a file - errors redirected matrixmadhan AIX 1 07-18-2005 05:12 AM
Saving a redirected file JSP UNIX for Dummies Questions & Answers 1 09-27-2001 07:52 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-24-2008
fongthai fongthai is offline
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
  #2 (permalink)  
Old 04-24-2008
frajer frajer is offline
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.
  #3 (permalink)  
Old 04-24-2008
uvrakesh uvrakesh is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 78
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
  #4 (permalink)  
Old 04-24-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
  #5 (permalink)  
Old 04-24-2008
fongthai fongthai is offline
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.
  #6 (permalink)  
Old 04-24-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
If you run it without redirection, do you see the results on your screen?

If you terminate it with ctrl-c, the output might still be buffered somewhere (weak hunch, might be wrong) -- if you let it run for long enough, does the output become visible?
  #7 (permalink)  
Old 04-24-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Tested here; looks like I'm right. If I hit ctrl-c after five seconds (enough to produce about two matching lines, I guess) the output file is empty. If I run it for a minute or so, I still don't get output in the file. Then all of a sudden, lots of output. It's output buffering at play.

If you have grep --line-buffered, try with that, just to see that it works. If you run it for long enough, you probably want to take it out again, though.
Closed Thread

Bookmarks

Tags
linux, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0