13

I have a script that streams a pattern in a single line (no linebreaks). I want to grep stock_ticker in that line and output it as soon as I have found one. Now the script is never ending and is essentially looping indefinitely.

One alternative I thought how I could work on this probably would be to split the input stream into lines and pipe it to grep. I understand that you can grep an output - however per my understanding I find everything from grep, sed, awk read line by line.

Anyway I can change this behavior and work on this?

./a.out | grep 'stock_ticker'

currently outputs Memory exhausted. This is because grep reads line by line before spitting output. I would want to change that behavior. Any ideas how?

2
  • Can you please be a little more specific by pasting the inputs, the script, the expected output kinda stuff....
    – neuron
    Jul 28, 2015 at 17:53
  • Imagine the script to be - main() { while(1) printf("stock_ticker one"); }
    – Phani
    Jul 28, 2015 at 18:06

1 Answer 1

15

Without line breaks, grep is buffering all of the input, so that it can show you the "line" where the string appears.

Two questions:

  • Do you need the adjacent content?
  • are there spaces or other characters separating the tokens?

If you don't need the context of the adjacent content and there are spaces separating tokens, just use tr to turn spaces into linefeeds:

./a.out | tr ' ' '\n' | grep 'stock_ticker'

If you want the adjacent content, just add one of the options -C -A or -B to the grep command. That lets grep show the "lines" before and/or after the ticker in the search pattern.

1
  • Yes I would need work on adjacent content. There are spaces and one alternative is to split the input into lines to enable grep read it. However I am not able to figure out how.
    – Phani
    Jul 28, 2015 at 18:06

Not the answer you're looking for? Browse other questions tagged .