0

I am using Debian Linux (in a BeagleBone Black). The program I am using prints a lot of information on screen. So far, I have been saving entire output in a text file, which is obviously taking up a large portion of the limited storage space. For example, I was doing something like this:

./exampleProgram > Output.txt

As I said, this file contains a lot of information, which I don't really need at this moment. I only care about the lines which starts with the words "Schedule request". I am looking for a way to save just those lines to the output file, and ignore rest. I tried following, but it did not work.

./exampleProgram | grep "Schedule request" > Output.txt

How can I do this?

leaRner
  • 3
  • 2
  • 1
    Why didn't it work? What did you get/not get that you expected? – AlG Jul 27 '17 at 14:54
  • Could you please post an excerpt of _exampleProgram_? – John Goofy Jul 27 '17 at 14:57
  • Because the file was empty, although there were lines present which started with "Schedule request" – leaRner Jul 27 '17 at 14:59
  • Felix, tail -f | grep works fine for me when I am applying it on a file. Right now, I am not reading a file, rather from the bash output, and want to save only lines of my interest. – leaRner Jul 27 '17 at 15:01
  • See [BashFAQ #9](http://mywiki.wooledge.org/BashFAQ/009). Short form: Buffering happens, so data is rewritten only when there's a large enough chunk ready. – Charles Duffy Jul 27 '17 at 15:05
  • Maybe try using the first line to redirect to a temporary file, grepping the temp file and redirecting to `Output.txt` – 17slim Jul 27 '17 at 15:05

1 Answers1

1

On a modern GNU platform:

stdbuf -oL ./exampleProgram \
  | grep --line-buffered "Schedule request" \
  > Output.txt
  • stdbuf -oL is a GNU tool which configures stdout to be unbuffered before running exampleProgram. (exampleProgram can override or ignore this setting, so its implementation matters).
  • grep --line-buffered tells GNU grep not to buffer sections larger than a single line either.
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Thanks! I also just figured about this option, also the unbuffer option. I was not aware of this buffering! – leaRner Jul 27 '17 at 15:13
  • `unbuffer` has a pretty heavyweight dependency chain -- it's part of `expect`, which requires a TCL interpreter; it's a large enough piece of software you wouldn't want to install it on a Raspberry Pi or a phone unless you intended to use it for something else as well. – Charles Duffy Jul 27 '17 at 15:15
  • Thanks! This is the solution I was looking for. – leaRner Jul 27 '17 at 15:34
  • Yes, I also figured out that, and also for some reason unbuffered was also not working as expected. – leaRner Jul 27 '17 at 15:35
  • One common reason it won't work is applying it to the wrong piece of a pipeline. For instance, `./exampleProgram | unbuffer grep "Schedule request"` might be what you need, instead of `unbuffer ./exampleProgram | grep "Schedule request"`, if it's only `grep` and not `exampleProgram` doing the buffering. – Charles Duffy Jul 27 '17 at 15:36
  • Yeah, probably that's the reason. I tried with putting unbuffered at the beginning. Anyway, I don't need it any more. The one you mentioned is quite simple and serves my purpose completely. Thanks again! – leaRner Jul 27 '17 at 15:41