![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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. |
|
||||
|
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? |
|
||||
|
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. |
![]() |
| Bookmarks |
| Tags |
| linux, ubuntu |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|