[英]Using awk to get a specific string in line
I have a java process containing multiple strings in the ps
output. 我有一个Java进程,在
ps
输出中包含多个字符串。 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. 我只想要
process.name=java1
而从ps
输出中不需要任何其他内容。 I was unable to find a tangible way to do so using awk or sed. 我无法找到使用awk或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. 我得到的输出是
ps -ef
out。
Is there a simpler way? 有没有更简单的方法?
Here is one way: 这是一种方法:
ps -ef | awk -F"process.name" '{split($2,a," ");print FS a[1]}'
process.name=java1
There is a simpler way: use grep -o
有一个更简单的方法:使用
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. 但是,这只会输出字符串“ process.name = java1”(可能多次,每个进程一次),这对我来说似乎毫无意义。 What are you really trying to do?
您到底想做什么?
尝试这样做:
ps -ef | grep -o 'process\.name=[a-z0-9]\+'
Using gnu-awk
: 使用
gnu-awk
:
ps -ef | awk -v s='process.name=' 'BEGIN{RS=s} !RT{print s $0}'
process.name=java1 -Djava.security.policy=security.policy
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: 这可能是您想要的,但是没有更多示例输入(
ps -ef
输出),并且在=
符号后对要匹配的内容进行了更好的描述,这只是一个猜测:
$ sed -r 's#.*[[:space:]]+-D(process\.name=[^[:space:]]+).*#\1#' file
process.name=java1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.