简体   繁体   中英

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. 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:

$ 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

PS: Cut default delimiter is tab, you need to declare with -d option space as delimiter in order cut to work with your data.

i just did this way

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%-*}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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