在一行中显示用户连接到的所有 ssh pids

display all the ssh pids where the users are connected to in one line

如何在一行中以逗号分隔显示用户连接的所有 ssh pid?

此命令以多行显示输出,我希望在一行中显示输出。

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

来自

1325
3255
2323
5321
3252

1325, 3255, 2323, 5321, 3252

谢谢!

您可能希望使用 pgrep 直接获取进程 ID:

$ pgrep ssh
1217
5305

这样,您就可以避免调用 ps aux 并解析其输出,它始终包含 grep 本身。

要将它们加入 , 分隔列表,请在 -s 串行模式下使用 paste

$ pgrep ssh | paste -s -d,
1217,5305

您可以在另一个管道中使用 sed 实用程序,因此命令为:

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

你实际上用逗号替换新行(最后一行除外)的地方。