我需要根据"ps-ef|grep";命令输出。在我们的组织中,用户正在尝试使用不同的命令和选项启动pgbouncer。我的要求是挑选pgbouncer二进制文件及其配置文件
这是一个例子:
[root@sl273pgb01 pavan]# ps -ef |grep pgbo
pgbounc+ 1655 1 0 Jan11 ? 00:15:05 /usr/bin/pgbouncer -d -q /etc/pgbouncer1/pgbouncer.ini
pgbounc+ 1678 1 0 Jan07 ? 00:15:05 /usr/bin/pgbouncer /etc/pgbouncer2/pgbouncer.ini -d -q
pgbounc+ 1699 1 0 Jan09 ? 00:15:05 /usr/bin/pgbouncer -d /etc/pgbouncer3/pgbouncer.ini -R
OP的努力:
ps -ef |grep pgbo |awk '{print $ 7 " " $8}'
从上面的输出,需要得到如下的输出
/usr/bin/pgbouncer /etc/pgbouncer1/pgbouncer.ini
/usr/bin/pgbouncer /etc/pgbouncer2/pgbouncer.ini
/usr/bin/pgbouncer /etc/pgbouncer3/pgbouncer.ini
请帮我怎样才能得到想要的输出。
根据您显示的示例,您可以尝试以下操作吗?这是不可行的,无论.ini
在ps命令中的位置如何。
ps -ef |
awk '
match($0,//usr/bin/pgbouncer/){
val=substr($0,RSTART,RLENGTH)
match($0,//etc/pgbouncer[0-9]+/pgbouncer.ini/)
print val,substr($0,RSTART,RLENGTH)
}
'
解释:添加以上详细解释。
ps -ef | ##Running ps -ef command and sending its output to awk command as an input here.
awk ' ##Starting awk program from here.
match($0,//usr/bin/pgbouncer/){ ##Using match function which matches /usr/in/pgbouncer anywhere in current line.
val=substr($0,RSTART,RLENGTH) ##Creating val which has sub string of matched above regex.
match($0,//etc/pgbouncer[0-9]+/pgbouncer.ini/) ##Using match function to match /etc/pgbouncer digits/pgbouncer.ini in current line.
print val,substr($0,RSTART,RLENGTH) ##Printing val value and sub string of matched regex here.
}
'
如果使用ps(-o(的输出格式选项,则无需使用awk,因此:
ps -eo pid,cmd | grep "pgbo"
在GNUawk
中测试
https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html
"如果regexp包含括号,则数组的整数索引元素被设置为包含与对应的带括号的子表达式"匹配的字符串部分;(
match()
函数(。
awk '{match($0,/(/usr/bin/pgbouncer).*(/etc/pgbouncer[0-9]+/pgbouncer.ini)/, arr); print arr[1], arr[2]}' file
/usr/bin/pgbouncer /etc/pgbouncer1/pgbouncer.ini
/usr/bin/pgbouncer /etc/pgbouncer2/pgbouncer.ini
/usr/bin/pgbouncer /etc/pgbouncer3/pgbouncer.ini