Reputation: 23
I have to extract a certain port number piped from a process status (ps) command and am not quite sure how to get it.
For example, if I had this line (ps -ef | grep "blahblahblah"):
xxremote -xcom.xx.management.xremote.port=9999 -xcom.xx.management.xxxremote.ssl=false
How can I extract the number "9999"? NOTE: This is one part of a very LONG line, and I cannot use, for example, awk, to count how many fields after the "=" sign. The number of = signs will change.
I tried using the cut command but I only know how to use it with single character delimiters which isn't what I need. I was thinking maybe awk or sed would do the trick? I am not to familiar with them. Thank you very much for the help
ps -ef | grep "blahblahblah" " | awk .... (or sed)
UPDATE: Clarified that the port number needs to be extracted form a single long line of text, and that using an awk command is not required.
Upvotes: 2
Views: 2994
Reputation: 360685
ps -ef | sed -n '/blahblahblah/s/^.*port=\([[:digit:]]\+) .*/\1/p'
Upvotes: 3
Reputation: 107090
Must you use Awk? sed
would be a better choice...
Oh well...
ps -ef | awk '/blahblahblah/ {
sub(".*port=", "")
sub(" .*", "")
print $0
}'
The sub(a, b)
command in awk substitutes the regular expression a
with string b
(which happens to be blank in my case. It takes $0 which represents the line returned.
As I said, sed
would be simpler:
ps -ef | sed -n '/blahblahblah/s/^.*port=\([^ ]*\).*/\1/p'
Notice I'm using sed
to select the line blahblahblah and to do the substitution.
Upvotes: 1
Reputation: 143152
This will do it:
(updated to filter only lines that contain xremote.port=
in case the data isn't filtered yet)
ps -elf | awk -F= '/xremote.port=/{print $2}' | awk '{print $1}'
The first awk
command splits the line on =
resulting in the 2nd field being:
9999 -xcom.xx.management.xxxremote.ssl
the second awk
command grabs the first field of this which is your number.
9999
Upvotes: 1