Skip to content
Advertisement

How do I grep or sed in a continuous stream of characters?

I have a program that output some text and then a continuous stream of characters:

JavaScript

I want to exit this program as soon as “A” is seen in the output and I tried:

JavaScript

However, it seems like grep doesn’t see the “A”s until the perl program exits.

I also tried with sed with no luck;

JavaScript

Any ideas how I can solve this?

Advertisement

Answer

The issue here is that both grep and sed process their input linewise unless they are told otherwise. One workaround is to insert newlines, e.g. with coreutils fold:

JavaScript

But this still has issues because of pipe-buffering. You can disable this with coreutils stdbuf:

JavaScript

Another alternative is to use dd, e.g.:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement