Return word before a matched word using sed

sedtext processing

I am trying to extract and print the word that occurs just before a specific word I am matching.
As an example,

There are 12 processes running.

Here I am using sed to search for the word processes and I would just like to print 12.

Is this possible using sed?

Best Answer

If you only need to handle that one line, you could use the sed command

sed -e 's/.* \([[:digit:]]\{1,\}\) processes running\./\1/'

For a slightly more robust approach, the following script will accept arbitrary input and only respond if something matched

sed -ne 's/.* \([[:digit:]]\{1,\}\) processes running\./\1/p'
Related Question