6

I can find the pid's of my program instances by issuing command like this:

pidof avconv

Which is giving me results

16616 16283 16279 16198 16175 16035 15073 14049 4922

But how can I output the same result line by line, like:

16616 
16283 
16279 
16198 
16175 
16035 
15073 
14049 
4922
1
  • 12
    Why? Note that command substitution or xargs will split as happily on space as it will on newline. Commented Mar 31, 2015 at 13:02

6 Answers 6

15

You could parse the output with sed, as suggested by @Sobrique, or with tr:

pidof avconv | tr ' ' '\n'

Another approach would be to use pgrep instead:

$ pgrep avconv16616 
16283 
16279 
16198 
16175 
16035 
15073 
14049 
0
8

You can just get it that way in the first place...

ps -C avconv -o pid=
0
5

You can always use sed to insert some linefeeds

pidof avconv | sed 's/ /\n/g'

although perhaps a bigger question is - why do you need them on separate lines? It may be that there's a more appropriate answer.

6
  • I do pidof avconv | tr ' ' '\n'
    – wurtel
    Commented Mar 31, 2015 at 12:58
  • I think it's 6 of one, a half dozen of the other when it's single character swaps.
    – Sobrique
    Commented Mar 31, 2015 at 12:59
  • 2
    Strictly speaking, tr should be a little bit faster since it's working at the character level and not parsing regular expressions. For example, on a 9MB file, sed took 55 seconds while tr took 19.
    – terdon
    Commented Mar 31, 2015 at 13:05
  • Oh I won't argue with larger data sets the efficiency matters. For smaller, I'd say familiarity is what matters. I posted the sed solution, because that's the first that came to mind :)
    – Sobrique
    Commented Mar 31, 2015 at 15:03
  • @Sobrique, thank you for the answer. Working perfectly. By the way i need the pids in separate lines, for special purpose
    – fortune
    Commented Mar 31, 2015 at 17:29
3

There are multiple ways to do it depending upon your need.

  1. If you are doing it in a shell script

    for line in $(pidof avconv)
    do
      echo $line
    done
    
  2. If you are looking for a oneliner

    pidof avconv |perl -pne 's|\s+|\n|g' 
    
2
  • Both options works perfectly and reliably... but it's a bit of an overkill :-) Commented Apr 4, 2015 at 11:49
  • @FranklinPiat you are correct. :)
    – fortune
    Commented Apr 4, 2015 at 17:40
3

If you're using pidof on a Linux system, it may be possible to tell the utility to use a newline as the delimiter instead of space using its -d option:

pidof -d $'\n' /usr/sbin/apache2
-1

Try this as well

ps ax | grep "program name, or part of it"

You will get the right answer, but also a meaningless entry like:

11424 pts/3    S+     0:00 grep --color=auto something

Using awk to show only PID's:

ps ax | grep "program name, or part of it"| awk '{print $1}'

That will give you the PID's, but also the PID of the meaningles entry. This entry is usually the last, so just leave it out.

2
  • Grepping ps is very unreliable, as you may grep on unxpected fields (substring match of program name, file path, process status, tty...). ps -C avconv would be a better option here. Commented Apr 4, 2015 at 11:43
  • True. It takes the paths into account on some occasions. Well it can do the job under most circuimatances.
    – WalyKu
    Commented Apr 4, 2015 at 12:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .