radicaled radicaled - 2 years ago 109
Bash Question

Split string in bash

I'm trying to monitor some hung threads in an application server and I got this line.

[5/30/16 11:31:18:501 CDT] 00000655 ThreadMonitor W WSVR0606W: Thread "MessageListenerThreadPool : 38" (00000655) was previously reported to be hung but has completed. It was active for approximately 964450 milliseconds. There is/are 0 thread(s) in total in the server that still may be hung.

I need to obtain the 0 in "There is/are 0 thread(s)".

I tried to split it using cut, but everything was a mess. I also tried to use a delimiter, but the string is too long.

How can I split it?

Answer Source

You can use sed to capture the number between "There is/are " and " thread" and remove everything else, like this:

sed 's/.*There is\/are \([0-9]\+\) thread.*/\1/' file
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download