Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top