way to print all the string till we get a space and a number

Is there any way to print all the string till we get a space and a number and store it a variable

for eg we have string java.io.IOException: An existing connection was forcibly closed by the remote host 12

All I want is to store "java.io.IOException: An existing connection was forcibly closed by the remote host" in a variable and "12" in another variable.

Plz give the solution with for loop, as I have lot of string like this.Plz help me on this.

Note: end of the string or seperation point is "space and a number".

hope below can give you some clue

echo "I am a man whose age is 30" | sed 's/\(.*\)\( [0-9][0-9]*\)/var1=\1 -- var2=\2/'

The solution is perfect.Thanks a lot.

But one problem here is that the string could be anything (dynamic string).

We cannot decide a string like in this case above.Plz help on that too.Thanks a ton guys.

My script o/p is as below

0 892
SetExpressCheckout 4
DoExpressCheckout Payment 4
0-211 3
GetExpressCheckoutDetails 4
3.auth error.INTERNAL.5005 1
2.NVPPAYMENTSERV INTERNAL 0(10002) 1
2.NVPPAYMENTSERV INTERNAL.5005 1

Table generation time : 0.61 ms
Cube clean time : 1.93 ms

i=0,the logic that I applied was store this o/p in a single variable called "col".
then col [i]=0 and col[i\+1]=892 so on and increment i with 2.

But the problem I found was with
DoExpressCheckout Payment
2.NVPPAYMENTSERV INTERNAL 0(10002) 1
2.NVPPAYMENTSERV INTERNAL.5005 1

as I wanted "DoExpressCheckout Payment" to be assigned to col [i]but instead "DoExpressCheckout" was assigned and the "Payment" was treated as a seperate variable.But I want this not happen.

I want the o/p to be parsed and when a condition like "space + number" is met then store that entire string to a variable.

Here is one solution based using ksh93 extended patterns but it can easily be converted into regular expressions for use in bash, sed, etc.

while IFS= read str
do
   col1=${str/*([[:print:]])([[:space:]])+([[:digit:]])/\1}
   col2=${str/*([[:print:]])([[:space:]])+([[:digit:]])/\3}
   printf "COL1: ${col1}   COL2: ${col2}\n"
done < file

Here is the output for the example list you provided

COL1: 0   COL2: 892
COL1: SetExpressCheckout   COL2: 4
COL1: DoExpressCheckout Payment   COL2: 4
COL1: 0-211   COL2: 3
COL1: GetExpressCheckoutDetails   COL2: 4
COL1: 3.auth error.INTERNAL.5005   COL2: 1
COL1: 2.NVPPAYMENTSERV INTERNAL 0(10002)   COL2: 1
COL1: 2.NVPPAYMENTSERV INTERNAL.5005   COL2: 1

Is the space and number always the last two things on every line?

vi file.sh ==>

while IFS= read str
do
col1=${str/([[:print:]])([[:space:]])+([[:digit:]])/\1}
col2=${str/
([[:print:]])([[:space:]])+([[:digit:]])/\3}
printf "COL1: ${col1} COL2: ${col2}\n"
done < input

vi input==>

0 892
SetExpressCheckout 4
DoExpressCheckout Payment 4
0-211 3
GetExpressCheckoutDetails 4
3.auth error.INTERNAL.5005 1
2.NVPPAYMENTSERV INTERNAL 0(10002) 1
2.NVPPAYMENTSERV INTERNAL.5005 1

now ./file.sh ==>

COL1: 0 892 COL2: 0 892
COL1: SetExpressCheckout 4 COL2: SetExpressCheckout 4
COL1: DoExpressCheckout Payment 4 COL2: DoExpressCheckout Payment 4
COL1: 0-211 3 COL2: 0-211 3
COL1: GetExpressCheckoutDetails 4 COL2: GetExpressCheckoutDetails 4
COL1: 3.auth error.INTERNAL.5005 1 COL2: 3.auth error.INTERNAL.5005 1
COL1: 2.NVPPAYMENTSERV INTERNAL 0(10002) 1 COL2: 2.NVPPAYMENTSERV INTERNAL 0(10002) 1
COL1: 2.NVPPAYMENTSERV INTERNAL.5005 1 COL2: 2.NVPPAYMENTSERV INTERNAL.5005 1

:confused: plz tell me what is wrong with the code.

What shell (and version number) are you using?

tcsh 6.13.00 (Astron) 2004-05-19

Plz tell me what should I do now?

I have c shell and bash shell also....

Plz help me out...

OK, here is one way of doing what you want to do using bash. It relies on sed using greedy matching.

while IFS= read str
do
   col2=`echo "$str" | sed -e 's/^.* //'`
   col1=${str%${col2}}
   printf "COL1: ${col1}   COL2: ${col2}\n"
done < file

cat file_having_java_strings | while read str
do
var1=${str##* } #will contain number part of the error string
var2=${str% *} # will contain the text part of the error string
echo "$var1"
echo "$var2"
done

Not sure.. If this is what u want

Thanks guys, the solution provided is working perfectly...

Thanks once again.:b: