简体   繁体   English

Shell cut grep命令

[英]Shell cut grep command

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

The above script gives the below output 上面的脚本给出了以下输出

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

I want to cut only the number 10481066 , I tried with number grep and other cut which is not working. 我只想剪切数字10481066 ,我尝试用数字grep和其他cut不起作用。 Can anyone suggest? 有人可以建议吗?

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

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

Just some alternatives to awk: 只是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"

All above return 10481066 以上所有收益10481066

PS: Cut default delimiter is tab, you need to declare with -d option space as delimiter in order cut to work with your data. PS:Cut的默认定界符是tab,您需要使用-d选项空间声明为定界符,以便剪切才能使用您的数据。

i just did this way 我只是这样

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

any way thanks for your help 谢谢你的帮助

I would do manipulating the final string, 我会处理最后的字符串,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM