0

How can I display all the ssh pids where the users are connected to in one line separated by comma ?

This command displays the output in multiple lines, I would like to have the output in one line.

ps aux | grep -i "ssh" | awk '{print $2}'

from

1325
3255
2323
5321
3252

To

1325, 3255, 2323, 5321, 3252

Thank You!

VGM
  • 69
  • 14

2 Answers2

1

You may want to use pgrep to get the processes IDs directly:

$ pgrep ssh
1217
5305

This way, you avoid calling ps aux and parsing its output, which will always contain the grep itself.

To join them on a ,-separated list, use paste on a -serial mode:

$ pgrep ssh | paste -s -d,
1217,5305
fedorqui
  • 275,237
  • 103
  • 548
  • 598
0

You could use sed utility in another pipe, so the command would be:

ps aux | grep -i "ssh" | awk '{print $2}' | sed ':a;{N;s/\n/, /};ba'

Where you are in fact replacing new lines (except the final one) by commas.