For each output of grep execute multiple commands

1

First:

sudo netstat -nptc | grep -oP "\K[0-9]*(?=/perl)"

The above gives me the pid as they are started, so it is constantly running.

Second:

For each output of the first command above I want to execute below:

ps aux | grep <output of first>

How can this be done?

Exocomp

Posted 2017-07-22T17:23:59.300

Reputation: 141

Answers

1

To send the output of one command into the command line of another command use xargs.

The complication here is that you need to start a pipeline, and for that use a shell. That can be done with something like:

sudo netstat -nptc | grep -oP "\K[0-9]*(?=/perl)" | xargs -l sh -c 'ps aux | grep "$0"'

(Reference)

Stephen Rauch

Posted 2017-07-22T17:23:59.300

Reputation: 2 455

I tried your solution but didn't work, it doesn't return anything, this part works sudo netstat -nptc | grep -oP "\K[0-9]*(?=/perl)" but the next part returns nothing. – Exocomp – 2017-07-25T17:36:08.590