You can use grep. <pstree command> | grep -P -o 'processTarget\([0-9]+\)' -P means use Perl regex -o mean only return the match man grep for more details If the goal is to get just the id you can continue this approach to return just the integer <pstree command> | grep...