问题
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