How to 'grep' a continuous stream?

前端 未结 12 1383
清酒与你
清酒与你 2020-11-22 11:15

Is that possible to use grep on a continuous stream?

What I mean is sort of a tail -f command, but with grep on t

12条回答
  •  北海茫月
    2020-11-22 11:30

    sed would be a better choice (stream editor)

    tail -n0 -f | sed -n '/search string/p'

    and then if you wanted the tail command to exit once you found a particular string:

    tail --pid=$(($BASHPID+1)) -n0 -f | sed -n '/search string/{p; q}'

    Obviously a bashism: $BASHPID will be the process id of the tail command. The sed command is next after tail in the pipe, so the sed process id will be $BASHPID+1.

提交回复
热议问题