我有下面的字符串,我想使用shell脚本从下面的字符串中获取2563
。
你能帮忙吗,如何获得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"
假设字符串存储在变量$s
中。
这是唯一的号码吗?然后
grep -Eo '[[:digit:]]+' <<< "$s"
会起作用。
或者使用Bash正则表达式:
re='request id ([[:digit:]]+)'
[[ $s =~ $re ]]
echo "${BASH_REMATCH[1]}"
您需要在字符串上使用grep -oE '[0-9]*'
,例如:
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]*'
输出:2563
id=$(awk -F" id | to " '{gsub(/ /,"", $2); print $2}' <<<$string)