Java application and grep - how can I independently handle the output of the command passed to grep with two regular expressions?

I have a Java application using SSH connection, remote execution of CLI commands. For a specific command, I would like to pass the output of the command through two regular expressions, I find two correlated subsets of the general output and return them back to my program.

For each of the two regular expressions: < RE1 > and < RE2 >, I would like to format the resulting outputs: < RE1out > and < RE2out via < action1 > and < action2 >, respectively; then return the final result, for example (please excuse the pseudo-shell script):

<command> | grep -e <RE1> -e <RE2> | (<REout1> given to <action1> and <REout2> given to   <action2>) yields <final_output>
+3
source share
2 answers

I would use this process, which temporarily creates a pair of temporary files, but still single-line:

command | 
awk '/RE1/ {print >> "tmp1"} /RE2/ {print >> "tmp2"}' && 
{ action1 < tmp1; action2 < tmp2; rm tmp1 tmp2; }
+1
source

try it

<command> | tee >(grep -e <RE1> | <action1>) >(grep -e <RE2> | <action2>)
+3
source

Source: https://habr.com/ru/post/1788843/


All Articles