How to get a number from a grepped sentence of a file?

I want get a number(ID) from a sentence which has been grepped from file using error number.

For Example:

#!/bin/ksh
echo "Enter RRS ID: "
read rrs
echo "Enter error number:"
read err
scp -pr ptc-avdbamdw102:/home/icsprd/M3logs/Accurate/logs/corp_post/$rrs.*.err.txt $HOME/daemon_mail/
cat $rrs.*.err.txt | grep "Error $err"

This will give output as follows:

Error 118173 in ratings for shelf/program ID 823197064: Currency cap is invalid for the rating dated 'Aug  1 2012  3:52PM'; check that currency of this instrument or program matches the currency of the domicile of the issuer.

From this sentence i have to get 823197064 number.

Please advise.:confused:

 
cat $rrs.*.err.txt | grep "Error $err"

change it to

awk -v err="$err" '/Error/ && $0~err {for(i=1;i<=NF;i++)if($i~/ID/){print $(i+1);next}}' $rrs.*.err.txt
1 Like

Another one:

awk '$0~e {if(match($0,/ID [0-9]+/)) print substr($0,RSTART+3,RLENGTH-3)}' e="Error $err" $rrs.*.err.txt

And the output will not have the colon generated by the previous solution.

1 Like

Thank you all.
It worked..
As i am learner of UNIX, Could you please explain code.

One more question,
If same error come more than one time for different ID numbers,than?/?
In other words grepped result would be more than one line than...???

Then the statements given to you should print one line of output for each matching line of input.

1 Like

If grepped setence is the duplicate, is there any way to remove it?
I have verified for above output it will give desired output for multiple IDs but with \n seperator, shall i able to change it to , seperator?

I'm not quite sure what you mean.

Please show an example of the input that shows the problem and output which shows the result you want.

output will be like as below.
As there are duplicate rows in a file, it will give me grepped result as below.
I want only one line instead.

 
343085,343085

Error 110132 in debt instrument header for instrument ID 343085: This instrument ID number does not exist.
Error 110132 in debt instrument header for instrument ID 343085: This instrument ID number does not exist.