从 Linux 生成 CSV 列表 'ps'
假设我有一个如下所示的 ps 命令:
ps -Ao args:80,time,user --sort time
它将给我一个以“空格”分隔的行集。一行可能看起来像这样,
paulnath -bash 00:00:00
我想说服 ps 用逗号(甚至制表符!)分隔,这样它就可以由其他语言自动处理。请注意,args 中可能有空格,因此,awking by field 本身不起作用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用以下语法来放置您自己的分隔符:
You can use the following syntax to put your own delimiter:
代码
输出
-(仅示例,与OP不同)
我不知道
ps
OP可能有什么变体,无论标志-Aco
是否有效 - 我只能谈谈我的经验macos 12.4
————————————————
事实上,将
OFS
设置为您喜欢的任何分隔符 - 我个人通常喜欢等号 [=
] 作为字段分隔符各个字段字符串可以完全不加引号,
unicodes
,甚至各种随机字节相反,基本上不需要转义任何内容,
即使对于单引号或双引号,
=
] 本身,然后只需查找随机的字节对或三元组来转义 [=
]与几乎任何其他
ascii
[[:punct:]]
相比,这是相当安全的选择,因为它是最不可能出现的字符之一shell
或任何regex
引擎中的特殊含义,同时仍可直接从QUERTY
键盘输入CODE
OUTPUT
- (sample only, not identical to OP's)
I dunno what variant of
ps
OP might have, whether flags-Aco
work or not - i can only speak about my experience onmacos 12.4
————————————————
In fact, set
OFS
to anything delimiter you like - personally I usually love equal sign [=
] as a field delimiterindividual field strings can go completely unquoted,
unicodes
, even all sorts of random bytesconversely, basically nothing needs to be escaped,
even for single- or double-quotes,
=
] itself, then just find a random pair or triplet of bytes to escape [=
]reasonably safe choice when compared to just about any other
ascii
[[:punct:]]
, since it's among the chars least likely to havespecial meanings in
shell
or anyregex
engine, while still directly type-able from aQUERTY
keyboard怎么样:
这对格式敏感,包括时间,并假设进程没有逗号。他们可以,但如果你想逃避这一点,显然会更复杂。
How about:
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.
对列重新排序,您可以将空格处理为命令值:
输出:
Reorder your columns and you can handle spaces into command values:
Output:
您可以组合多个 AIX FORMAT DESCRIPTORS(允许 printf 样式格式,但提供有限的字段子集)和 STANDARD FORMAT SPECIFIERS(允许完整的字段集)获取具有任意分隔符的字段的任意组合:
You can combine multiple AIX FORMAT DESCRIPTORS (which allow printf-style formatting, but provide a limited subset of fields) and STANDARD FORMAT SPECIFIERS (which allow the full set of fields) to get any combination of fields with any separator:
您可能想从
/proc/[0-9]*/
获取所需的信息。我认为您会发现它比 ps 的输出更易于编程访问。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.