简体   繁体   中英

Using awk to get a specific string in line

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?

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

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?

尝试这样做:

ps -ef | grep -o 'process\.name=[a-z0-9]\+'

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM