문제

I have been trying to extract specific strings from the output in Linux

For example:

ps -eo pid,args | grep PRD_ | egrep startscen.sh | more

gives the following output

http://i.imgur.com/reS7wZ1.png

(Full-size image: http://i.imgur.com/reS7wZ1.png)

I am aware awk, sed, tr can be used to extract details like PID but I am not sure how to write a query to get exactly the pid of the row where the fourth column has a specific string like 'PROCESS_ALL_BETS'

Or how do I extract every character after _NAME=?

도움이 되었습니까?

해결책

Awk to the rescue.

ps -eo pid,args | awk '/PRD_/ && /startscen\.sh/ && $4 ~ /PROCESS_ALLBETS/'

(In the image, you have PROCESS_ALLBETS, so I guess that's what you actually want, even though your text says PROCESS_ALL_BETS.)

This selects for printing every line which matches all the following conditions:

  • /PRD_/ -- there is a "PRD_" somewhere in the line. Maybe you would tighten this to something like $6 ~ /^-NAME=PRD_/ to only match on the beginning of the sixth field.
  • /stratscen\.sh/ -- there is a match for this regex somewhere on the line. Again, for improved precision, you might want to change this to $3 ~ /startscen\.sh/ or even $3 == "startscen.sh" if you only want exact matches.
  • $4 ~ /PROCESS_ALLBETS/ -- the fourth field matches this regular expression.

The above will simply print all matching lines. To print just the first field and the eight field with the prefix -SESSION_NAME= removed, add something like

{ n=$8; sub(/^-SESSION_NAME=/,"",n); print $1, n }

just before the closing single quote.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top