Grep on single line

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?

Asked By: Phani

||

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.

Answered By: Tim B
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.