Pic a value from a line at runtime


BIP in report_one_acnt> **SUCCESSFUL** BILL 25895519 ACCOUNT 7615

hi i have a line above.. i need to pick the value 25895519 at runtime.

this number can be of different length but the number will always appear in this this line. and between the words BILL and ACCOUNT

**SUCCESSFUL** this is also constant in the line.

how can i pick this value and store in a variable

You can use "sed" to manipulate the line. The following expression will pick up anything between "BILL" and "ACCOUNT":

echo "BIP in report_one_acnt> **SUCCESSFUL** BILL 25895519 ACCOUNT 7615" |\
sed 's/^.*BILL //; s/ ACCOUNT.*$//'

The first regexp cuts away everything before and including "BILL " (with a space following), the second cuts everything from " ACCOUNT" (with a leading blank) to the end of line, which leaves solely the number.

Still, this might have to be checked if it is indeed a number (for instance, "...BILL FOOBAR ACCOUNT..." might break whatever follows, because the extracted "FOOBAR" is NOT a number). You can do this by:

# catch the result in a variable
var="$( echo "BIP in report_one_acnt> **SUCCESSFUL** BILL 25895519 ACCOUNT 7615" |\
        sed 's/^.*BILL //; s/ ACCOUNT.*$//' \
      )

# test if "$var" contains only digits:
if [ -n "$(echo "$var" | sed 's/[0-9]//g)" ] ; then
     echo "$var is not a number"
else
     echo "$var is a number"
fi

I hope this helps.

bakunin
[/code]

Hello,

This solution will help you i case the field/column number is fix for the Digits required by you. Also lets say your data is stored in test11 file.

 
$ cat test11
BIP in report_one_acnt> **SUCCESSFUL** BILL 25895519 ACCOUNT 7615

$ awk '{print$6}' test11
25895519
$

 

Please let us knnow if we can help further on same.

Thanks,
R. Singh

1 Like

THANKS FOR ALL YOUR HELP
I actually came up with my solution

BILL_REF=`grep "**SUCCESSFUL**" bip.txt | cut -d " " -f 6`

Is awk slower than grep?