如何从 shell 脚本中的字符串中获取特定字符串
how to get specific string from string in shell script
我有下面的字符串,我想使用 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(/ /,"", ); print }' <<<$string)
我有下面的字符串,我想使用 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(/ /,"", ); print }' <<<$string)