Home > other >  how to get specific string from string in shell script
how to get specific string from string in shell script

Time:03-31

I've below string, I would like to get 2563 from the below string using shell script.

Could you please help, how to get the number which is after request id

"requested producer access with request id 2563 to product exampledomain.test-dev.test.nonprod.com for user : test-user on product test-product-validation-required"

CodePudding user response:

Assuming the string is stored in a variable $s.

Is it the only number? Then

grep -Eo '[[:digit:]] ' <<< "$s"

would work.

Or using a Bash regex:

re='request id ([[:digit:]] )'
[[ $s =~ $re ]]
echo "${BASH_REMATCH[1]}"

CodePudding user response:

You need to use grep -oE '[0-9]*' on string, like:

data="requested producer access with request id 2563 to product exampledomain.test-dev.test.nonprod.com for user : test-user on product test-product-validation-required"

echo $data | grep -oE '[0-9]*'

Output: 2563

  • Related