I combine top and pidof command:

top -p $(pidof <process_name>)

If pidof <process_name> return one id top command works. But if pidof <process_name> more than one ids top command does not work.

pidof returns ids with one space between them like : 123 124 125

If I can get these ids with comma like 123, 124, 125 top command will work. How can I change output format of pidof?

I know awk was used for formatting text data. But I can not find how can I use awk for result of shell command.


Using bash:

top -p "$(pname="$(pidof <process_name>)"; echo "${pname// /,}")"

Using tr:

top -p "$(pidof <process_name> | tr ' ' ',')"

Using sed:

top -p "$(pidof <process_name> | sed 's/ /,/g')"

Try this one using awk:

top -p $(pidof <process_name>|awk '{gsub(/ /, ","); print }')

You can try this one:

top -p "$(pidof <process_name> | awk 'OFS="," { $1 = $1; print; }')"

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.