Shell cut grep command


req=`bxp report change-summary $startDate $startDate  -iad -y | grep -A2 "Request ID" 

上面的脚本给出了以下输出

Request ID ------------ 10481066

我只想10481066削减数字,我尝试使用数字grep和其他不起作用的cut。谁能建议?

假设您的输出Request ID ------------ 10481066都在一行中,您可以将grep替换为以下awk命令:

req=$(bxp report change-summary $startDate $startDate -iad -y|awk '/Request ID/{print $NF}')

只是awk的一些替代方案:

$ egrep -o '[0-9]+' <<<"This is a line with Request ID ------------ 10481066"
$ cut -d' ' -f4 <<<"Request ID ------------ 10481066"
$ egrep -o '[0-9]+$' <<<"This is a line with number 35546 with Request ID ------------ 10481066"

以上所有返回10481066

PS:剪切默认分隔符是选项卡,您需要使用-d选项空间作为分隔符进行声明,以便剪切以处理您的数据。

我就是这样做的

要求=bxp report change-summary $startDate $startDate -iad -y | grep -A2 "Request ID" | grep -E "^[0-9]"

无论如何感谢您的帮助

我会操作最后一个字符串,

req="Request ID ------------ 10481066"
result=${req%-*}

最新更新