尝试加入 ps 和 pwdx linux 命令的输出 [英] Trying to join output from ps and pwdx linux commands
问题描述
我正在尝试连接 ps 和 pwdx 命令的输出.谁能指出我命令中的错误.
I am trying to join output from ps and pwdx command. Can anyone point out the mistake in my command.
ps -eo %p,%c,%u,%a --no-headers | awk -F',' '{ for(i=1;i<=NF;i++) {printf $i",
"} ; printf pwdx $1; printf "\n" }'
我希望每一行的最后一列是进程目录.但它只是显示 $1 的值而不是命令输出 pwdx $1
I expect the last column in each row to be the process directory. But it just shows the value of $1 instead of the command output pwdx $1
这是我的输出样本(1 行):
This is my output sample (1 row):
163957, processA , userA , /bin/processA -args, 163957
我期待
163957, processA , userA , /bin/processA -args, /app/processA
谁能指出我可能遗漏了什么
Can anyone point out what I may be missing
推荐答案
试试这个:
ps -eo %p,%c,%u,%a --no-headers | awk -F',' '{ printf "%s,", $0; "pwdx " $1 | getline; print gensub("^[0-9]*: *","","1",$0);}'
说明:
awk '{print pwdx $1}'
将连接 awk 变量 pwdx
(为空)和 $1
(pid).因此,实际上,您在输出中只获得了 pid
.
awk '{print pwdx $1}'
will concatenate the awk variable pwdx
(which is empty) and $1
(pid). So, effectively, you were getting only the pid
at the output.
为了运行一个命令并得到它的输出,你需要使用这个 awk
结构:
In order to run a command and gets its output, you need to use this awk
construct:
awk '{"some command" | getline; do_something_with $0}'
# After getline, the output will be present in $0.
#For multiline output, use this:
awk '{while ("some command" | getline){do_something_with $0}}'
# Each individual line will be present in subsequent run of the while loop.
这篇关于尝试加入 ps 和 pwdx linux 命令的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!