Grep into variable and maintain stdout?

浪子不回头ぞ 提交于 2020-01-24 00:53:07

问题


I've got a long running process and I want to capture a tiny bit of data from the big swath of output.

I can do this by piping it through grep, but then I can't watch it spew out all the other info.

I basically want grep to save what it finds into a variable and leave stdout alone. How can I do that?


回答1:


With tee, process substitution, and redirection:

{ var=$(cmd | tee >(grep regexp) >&3); } 3>&1

Note that if grep exits before cmd (GNU grep provides some flags that might cause that, and you might want to use them), cmd might catch SIGPIPE and fail/halt based on its internal signal handling mechanism. To prevent it; for GNU tee, use -p flag; and for other implementations, run tee in a subshell in which SIGPIPE is trapped & ignored.




回答2:


There isn't any way to use a variable like that. It sounds like you want to write your own filter:

./some_long_program | tee >(my_do_stuff.sh)

Then, my_do_stuff.sh is:

#!/bin/bash
while read line; do
    echo "$line" | grep -q 'pattern' || continue
    VARIABLE=$line   # Now it's in a variable
done

If you have the storage space, this is probably more like what you want:

./some_long_program | tee /tmp/slp.log

Then, simply:

grep 'pattern' /tmp/slp.log && VARIABLE=true

or:

VARIABLE=$(grep 'pattern' /tmp/slp.log)

This will let you run the grep at any time. I don't think the variable really adds anything though.

EDIT:

@mpen Based on your last answer above, it sounds like you want to use xargs. Try:

(echo 1 ; sleep 5 ; echo 2) | xargs -L1 echo got:

The -L1 will run the command for every instance found, otherwise it grabs lots of stdin and passes them all (up to some maximum) to the command at once. You'll still want to use this via tee if you want to see all the command output as well:

./some_long_program | tee >(grep 'pattern' | xargs -L1 ./my_do_stuff.sh)



来源:https://stackoverflow.com/questions/58966324/grep-into-variable-and-maintain-stdout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!