質問

When using: "ps -fu ${USER} | grep address" I can find the java process and also, the debug port number which appears as part of the process command line. My question, is how do I extract the port number from the command line which contains many characters. The question can be summed up to how can I extract a word between 2 strings. I have been looking up how to do it with grep, awk or sed. But all my trial ended with displaying the entire process command line and not just the java port number.

Thanks ahead.

役に立ちましたか?

解決

You can extract the debug port with sed:

ps -fu ${USER} | grep address | sed 's/.*address=\([0-9]*\).*/\1/'

Update:

The output from ps may include the grep command itself. You can use grep again to filter that out:

ps -fu ${USER} | grep address | sed 's/.*address=\([0-9]*\).*/\1/' | grep ^\d
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top