Generating a CSV list from Linux 'ps'

一个人想着一个人 提交于 2019-11-27 02:21:48

问题


Suppose I have a ps command that looks like this:

ps -Ao args:80,time,user --sort time 

It will give me a "space" separated set of rows. A row might look like this

paulnath -bash 00:00:00

I would like to convince ps to delimit by commas(or tabs even!), such that it can be processed automagically by other languages. Please note that args will probably have spaces in it, so, awking by field won't per se work.


回答1:


You can use the following syntax to put your own delimiter:

ps -Ao "%U,%t,%a"



回答2:


How about:

ps -Ao args:80,time,user --sort time | 
sed 's/\([[:digit:]]\{2\}:\)\{2\}[[:digit:]]\{2\}/,\0,/'

This is sensitive to the format, including the time, and assumes processes don't have commas. They can, but if you want to escape that it's obviously more complicated.




回答3:


You might want to get the information you need from /proc/[0-9]*/. I think you'll find it more programmatically accessible than the output of ps.



来源:https://stackoverflow.com/questions/3114741/generating-a-csv-list-from-linux-ps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!