从 Linux 生成 CSV 列表 'ps'

发布于 2024-09-06 15:08:03 字数 265 浏览 8 评论 0原文

假设我有一个如下所示的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

清风无影 2024-09-13 15:08:03

您可以使用以下语法来放置您自己的分隔符:

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

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

ps -Ao "%U,%t,%a"
慕烟庭风 2024-09-13 15:08:03

代码

ps -Aco time,user,command | mawk NF=NF OFS=','

      # lower-case -o for basic columns
      # upper-case -O for info overloading

输出
-(仅示例,与OP不同)

TIME,USER,COMMAND
20:13.26,root,launchd
3:53.13,root,logd
0:32.95,root,UserEventAgent
0:04.87,root,uninstalld
6:58.58,root,fseventsd
0:02.48,root,mediaremoted
4:02.23,root,systemstats

我不知道ps OP可能有什么变体,无论标志-Aco是否有效 - 我只能谈谈我的经验macos 12.4

————————————————

事实上,将 OFS 设置为您喜欢的任何分隔符 - 我个人通常喜欢等号 [ = ] 作为字段分隔符

  • 各个字段字符串可以完全不加引号,

    • 即使它有空格、制表符、unicodes,甚至各种随机字节
  • 相反,基本上不需要转义任何内容,
    即使对于单引号或双引号,

    • 除非字段文本包含 [ = ] 本身,然后只需查找随机的字节对或三元组来转义 [ = ]
  • 与几乎任何其他 ascii [[:punct:]] 相比,这是相当安全的选择,因为它是最不可能出现的字符之一
    shell任何 regex 引擎中的特殊含义,同时仍可直接从 QUERTY 键盘输入

mawk NF=NF OFS==
                \
                 yields
                /
0:00.03=root=colorsyncd
0:54.30=root=searchpartyd
0:00.57=_appleevents=appleeventsd
0:00.92=root=findmydeviced
0:30.52=_networkd=symptomsd

CODE

ps -Aco time,user,command | mawk NF=NF OFS=','

      # lower-case -o for basic columns
      # upper-case -O for info overloading

OUTPUT
- (sample only, not identical to OP's)

TIME,USER,COMMAND
20:13.26,root,launchd
3:53.13,root,logd
0:32.95,root,UserEventAgent
0:04.87,root,uninstalld
6:58.58,root,fseventsd
0:02.48,root,mediaremoted
4:02.23,root,systemstats

I dunno what variant of ps OP might have, whether flags -Aco work or not - i can only speak about my experience on macos 12.4

————————————————

In fact, set OFS to anything delimiter you like - personally I usually love equal sign [ = ] as a field delimiter

  • individual field strings can go completely unquoted,

    • even if it has spaces, tabs, unicodes, even all sorts of random bytes
  • conversely, basically nothing needs to be escaped,
    even for single- or double-quotes,

    • except when field texts include [ = ] 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 have
    special meanings in shell or any regex engine, while still directly type-able from a QUERTY keyboard

mawk NF=NF OFS==
                \
                 yields
                /
0:00.03=root=colorsyncd
0:54.30=root=searchpartyd
0:00.57=_appleevents=appleeventsd
0:00.92=root=findmydeviced
0:30.52=_networkd=symptomsd
猥琐帝 2024-09-13 15:08:03

怎么样:

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

这对格式敏感,包括时间,并假设进程没有逗号。他们可以,但如果你想逃避这一点,显然会更复杂。

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.

自由如风 2024-09-13 15:08:03

对列重新排序,您可以将空格处理为命令值:

ps -Ao time,user,args:80 --sort time | awk '{
  for (i=1; i<=NF; i++) {
    if(i==3) printf ","
    if (i<3){
      if(i > 1) printf ","
    
      printf "%s", $i
    }else if (i>=3){
      printf "%s ", $i
    }
  }
  printf "\n"
}'

输出:

在此处输入图像描述

Reorder your columns and you can handle spaces into command values:

ps -Ao time,user,args:80 --sort time | awk '{
  for (i=1; i<=NF; i++) {
    if(i==3) printf ","
    if (i<3){
      if(i > 1) printf ","
    
      printf "%s", $i
    }else if (i>=3){
      printf "%s ", $i
    }
  }
  printf "\n"
}'

Output:

enter image description here

依 靠 2024-09-13 15:08:03

您可以组合多个 AIX FORMAT DESCRIPTORS(允许 printf 样式格式,但提供有限的字段子集)和 STANDARD FORMAT SPECIFIERS(允许完整的字段集)获取具有任意分隔符的字段的任意组合:

ps -A -o "%p," -o rss -o ",%c,%a"

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:

ps -A -o "%p," -o rss -o ",%c,%a"
娇妻 2024-09-13 15:08:03

您可能想从 /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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文