问题
I have a java process containing multiple strings in the ps
output. I just want a particular string out of it.
For example, I have
root 5565 7687 0 Nov20 ? 00:00:54 /bin/java -Xms256m -Xmx512m -XX:MaxPermSize=128m -XX:PermSize=256m -XX:MaxPermSize=256m -Dstdout.file=/tmp/std.out -da -Dext.dir.class=profiles/ -Dprocess.name=java1 -Djava.security.policy=security.policy
I want just want process.name=java1
and require nothing else from the ps
output. I was unable to find a tangible way to do so using awk or sed.
I am tried to use:
ps -ef | grep java1 | grep -v grep | awk '/process.name/ {print $0}'
The output I get is the ps -ef
out.
Is there a simpler way?
回答1:
Here is one way:
ps -ef | awk -F"process.name" '{split($2,a," ");print FS a[1]}'
process.name=java1
回答2:
There is a simpler way: use grep -o
ps -ef | grep -o 'process.name=java1'
However that will only output the string "process.name=java1" (possibly multiple times, once for each process) which to me seems a little pointless. What are you really trying to do?
回答3:
Try doing this :
ps -ef | grep -o 'process\.name=[a-z0-9]\+'
回答4:
Using gnu-awk
:
ps -ef | awk -v s='process.name=' 'BEGIN{RS=s} !RT{print s $0}'
process.name=java1 -Djava.security.policy=security.policy
回答5:
This MIGHT be what you want but without more sample input (ps -ef
output) and a better description of what it is you want to match after the =
sign it's just one more guess:
$ sed -r 's#.*[[:space:]]+-D(process\.name=[^[:space:]]+).*#\1#' file
process.name=java1
来源:https://stackoverflow.com/questions/27153582/using-awk-to-get-a-specific-string-in-line