I have the following long one liner which essentially provides a list of PIDs with a '|' delim character

echo $(lsof -p $(pgrep -f dosemu | tr '\012' ,) | grep '/media/datadrv' | awk '{print $2" - " $9}' | grep 'DBASE1.RES' | awk '{print $1}') | sed 's/\s\+/|/g'

this output something similar to 19066|19500

What I want to do is use this to feed into the pstree command using egrep.

for example the following command gives me what i need

pstree -p | egrep '19066|19500'

What I cant figure out is how to feed the results of the first command into the second.

Update Thanks to Goro's answer the complete one liner was :-

pstree -p | egrep $(echo $(lsof -p $(pgrep -f dosemu | tr '\012' ,) | grep '/media/datadrv' | awk '{print $2" - " $9}' | grep 'DBASE1.RES' | awk '{print $1}') | sed 's/\s\+/|/g')
  • Hellow @l0ckm4. If I understand correctly, you would like to feed 19066|19500 to the command pstree -p I mean, you would like to feed the numbers in the flag -p correct? – user88036 Sep 26 '18 at 10:42

You can give the linear a variable name

var=$(echo $(lsof -p $(pgrep -f dosemu | tr '\012' ,) | grep '/media/datadrv' | awk '{print $2" - " $9}' | grep 'DBASE1.RES' | awk '{print $1}') | sed 's/\s\+/|/g')

Then feed var into pstree as follows:

pstree -p $var

If you would like to feed one of the numbers 19066|19500, you can use sed to convert | to tab \t, then awk the number of interest as follows:

first_number=$(echo ${var} | sed 's/|/\t/g' | awk '{print $1}' )
 pstree -p ${first_number}
  • 1
    Thanks Goro - I made it a one liner thanks to your answer – l0ckm4 Sep 26 '18 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.