Regular expression query in AWK

Hi,
I have a string like this-->"After Executing service For 10 Request"
in this string i need to extract "10".
the contents of the string is variable and "10" appears before "For" and after "Request" i.e, in this format "For x Request"
I need to extract the value of x. How to do this in AWK?

Regards,
Omprasad

Try:

echo "${string}"|awk '/After Executing service For .* Request/ {VAL=$5;print VAL}'

Try this .....

Hi,
The solution that have been proposed may not solve my problem because the position of x i.e 10 in this case may not always be 5th word in the string.
Only thing that is constant here is the pattern "For X Request".
First I need to find this pattern and then extract x from it.

Use:

echo "${string}"|awk '/For .* Request/ {for (i=0;i<=NF;i++){if ($i == "For" && $(i+2) == "Request"){VAL=$(i+1)}}print VAL}'

That works....thanks Klashxx.
But how to do the same inside a script file. I have the entire string in a variable. :confused:

Try using expr...

a="After Executing service For 10 Request"
b=$(expr match "$a" ".*For \(.*\) Request.*")
echo $b

Could u post what u have?
I mean your script and your input file/string.
Thx

... or pattern matching to remove the leading and trailing text

$ a="After Executing service For 10 Request"
$ a=${a##*"For "}
$ a=${a%" Request"}
$
$ print $a
10

what if the number is at the end of the line ....
for eg:

After Executing service For Request: 10

and this phrase is in a file

thnks,

mercury

sed -n 's/.* *For \([0-9][0-9]*\) *Request *.*/\1/p' file