Jad*_*ias 8 linux process sed regular-expressions
我知道
ps ax
Run Code Online (Sandbox Code Playgroud)
返回 pid
1 ? Ss 0:01 /sbin/init
2 ? S< 0:00 [kthreadd]
3 ? S< 0:00 [migration/0]
Run Code Online (Sandbox Code Playgroud)
我所需要的只是清理这些字符串,但我无法使用 sed 来完成,因为我无法编写正确的正则表达式。你可以帮帮我吗?
Kyl*_*ndt 19
使用 ps 输出格式:
ps -A -o pid
命令的输出格式是最好的选择。o 选项控制输出格式。我在下面列出了一些参数,其余的请参阅'man ps'(使用多个它会是-o pid,cmd,flags
)。
KEY LONG DESCRIPTION
c cmd simple name of executable
C pcpu cpu utilization
f flags flags as in long format F field
g pgrp process group ID
G tpgid controlling tty process group ID
j cutime cumulative user time
J cstime cumulative system time
k utime user time
o session session ID
p pid process ID
Run Code Online (Sandbox Code Playgroud)
Awk 或 Cut 会更好地获取列:
通常,您不希望使用正则表达式来选择第一列,您希望通过管道将其剪切或 awk 切出第一列,例如:
ps ax | awk '{print $1}'
Run Code Online (Sandbox Code Playgroud)
正则表达式是一个选项,如果不是最好的:
如果你要使用正则表达式,它可能是这样的:
ps ax | perl -nle 'print $1 if /^ *([0-9]+)/'
Run Code Online (Sandbox Code Playgroud)
$1 仅打印括号中匹配的内容。^ 将 锚定到行的开头。空格星号表示允许在数字前使用可选的空格字符。[0-9]+ 表示一位或多位数字。但我不会为这个特定任务推荐正则表达式,明白为什么吗?:-)
使用-o开关获得 cust 格式输出
ps -o pid
Run Code Online (Sandbox Code Playgroud)
正如您明确要求的那样,使用 sed 的坏方法可能是
ps -ax | sed 's#^\( *[0-9]\+\) .*$#\1#'
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
31768 次 |
最近记录: |