1833433 Members
3096 Online
110052 Solutions
New Discussion

Re: Parsing a string

 
SOLVED
Go to solution
dude70
Occasional Advisor

Parsing a string

Hi I have a big sting like

" Hi this is a big string and i am trying to parse it this contains values v1(100) and v2(50)"

I want to parse it using unix shell script to 2 strings like
val1=100
val2=50
can you pl. help me.

Thanks
17 REPLIES 17
Michael Schulte zur Sur
Honored Contributor

Re: Parsing a string

Hi,

it should work with this:
val1=100
expr "Hi this is a big string and i am trying to parse it this contains values v
1(100) and v2(50)" : ".*\(${val1}\)"

greetings,

Michael
dude70
Occasional Advisor

Re: Parsing a string

Hi Michael ,

Thanks for the reply. It is actually a string s1 that i am getting in script from another operation. I want to extract the values 100 and 50 and assign it to another string s2 and s3 resp.


Thanks.
Rodney Hills
Honored Contributor

Re: Parsing a string

Try this-

set -- `echo "$x" | sed -e 's/[^(]*(\([0-9]*\))/ \1/g'`
val1=$1
val2=$2

Where $x is your string.
sed command changes strings to " 100 50"
set -- will set command parameters from sed command.
Then $1 is first value, $2 is second

HTH

-- Rod Hills
There be dragons...
dude70
Occasional Advisor

Re: Parsing a string

Hi,

Thanks for the reply. I won't work with that, because the string length may vary each time. The following is a sample only. Thing is v1 and v2 will be same. so I want to extract values from v1 and v2 and assign it to s2=100 and s3=50.

s1 ="Hi this is a big string and i am trying to parse it this contains values v1(100) and v2(50) it goes on..."



Rodney Hills
Honored Contributor

Re: Parsing a string

My solution doesn't matter on the size of the string or where the values are positioned. It will take the first number found in parenethis assign to val1 and take the second number found in parenthesis and assign to val2.

It is based on relative position of the numbers within the string.

Are you saying you want the text preceeding the number to be used to determine what variable name to be assigned (ie v1 goes to val1, v2 to val2)?

-- Rod Hills
There be dragons...
dude70
Occasional Advisor

Re: Parsing a string

Hi Rodney,

Thanks for the reply.
1. Actually it doesn't work that way. The expression you gave reads each string from the begining and assigns to $1..etc. I tested this. It does not read from the braces.

2. yes like you said I want to take the value v1(100) and put that in a string s1 and v2(50) in s2. ie. s1=100 and s2=50.

Thanks.
curt larson_1
Honored Contributor

Re: Parsing a string

give this a try

#!/usr/bin/ksh

print "$string" |
awk '/\([0-9]+\) {
for ( i=1;i<=NF;i++;) {
if ( $i ~ /[a-zA-Z0-9]+\([0-9]+\).*/ ) {
split($i,a,"(");
split(a[2],b,")");
printf("%s = %s\n",a[1],b[1]);
}}}'
curt larson_1
Honored Contributor

Re: Parsing a string

2. yes like you said I want to take the value v1(100) and put that in a string s1 and v2(50) in s2. ie. s1=100 and s2=50.

as long as v1 is before v2, you could do this

print "$string" |
awk '/\([0-9]+\) {
for ( i=1;i<=NF;i++;) {
if ( $i ~ /[a-zA-Z0-9]+\([0-9]+\).*/ ) {
split($i,a,"(");
split(a[2],b,")");
printf("%s\n",b[1]);
}}}' |
read s1 s2 junk
print "s1 = $s1\ns2 = $s2"
Rodney Hills
Honored Contributor

Re: Parsing a string

When you say it doesn't work, does it not set val1 and val2 to the numbers from the string? For instance-

$ x="Hi this is a big string and i am trying to parse it this contains values v1(100) and v2(50)"
$ set -- `echo "$x" | sed -e 's/[^(]*(\([0-9]*\))/ \1/g'`
$ val1=$1
$ val2=$2
$ echo $val1
100
$ echo $val2
50

I run the above and I get the 2 numbers you want out of the string...

-- Rod Hills
There be dragons...
dude70
Occasional Advisor

Re: Parsing a string

Hi,
Thanks. It works. I had some other problem in the script.

Thanks.
dude70
Occasional Advisor

Re: Parsing a string

Hi Rodney,

The value of the big string I am obtaining is from running a process.

I get that by

s1= `echo execprocess | grep..... etc`

Then I was using this string against the expr. you gave.

set -- `echo "$s1" | sed -e 's/[^(]*(\([0-9]*\))/ \1/g'`


It looks like this string contains lots of sp. chars and line breaks etc.

My question is how can I make this string a coherent one to fit into your expr.

Thanks.
dude.
Rodney Hills
Honored Contributor
Solution

Re: Parsing a string

Oh, the plot thickens...

Are you saying that s1 will contain multiple lines in it?

s1= `echo execprocess | grep..... etc`

If that be the case, then I recommend you don't put the results of execprocess into "s1". Rather put the lines to a file like /tmp/myoutput. Then in your shell script, loop through reading one line at a time into variable s1 from /tmp/myoutput and then you can parse each line individually.

If this starts getting more complicated, I would recommend you use a tool like "awk" or better yet "perl" to process the output from execprocess.

Good Luck

-- Rod Hills
There be dragons...
Michael Schulte zur Sur
Honored Contributor

Re: Parsing a string

Hi,

here is another try. Assign the output to s1.

Val1=`expr ${s1} : ".*v1.\($[0-9]\)"`
Val2=`expr ${s1} : ".*v2.\($[0-9]\)"`

hows that? Got no chance to test it however.

greetings,

Michael

Michael Schulte zur Sur
Honored Contributor

Re: Parsing a string

Oops, too tired!!

Val2=`expr ${s1} : ".*v2.\([0-9]*\)"`

greetings,

Michael
Elmar P. Kolkman
Honored Contributor

Re: Parsing a string

Why not this way: strip all text outsite the brackets, leaving only the result string in the two variables (and remember the space to be put between the variables:

strip=$(echo $s1 | sed -e 's/^[^(]*(//' -e 's/)[^(]*(/ /g' -e 's/).*$//')

Then it is easy to set your variables:
set -- $strip
v1=$1
v2=$2

You can also test the number of variables in string s1 this way, for instance. It could be more or less then two and this sed instruction should still work... though the assignment will not set v2 or ignore extra values.
Every problem has at least one solution. Only some solutions are harder to find.
dude70
Occasional Advisor

Re: Parsing a string

I have fixed the issue.

Thank you all guys!
Rodney Hills
Honored Contributor

Re: Parsing a string

Dude,

Can you share your solution with the rest of the forum?

Then we all benefit...

-- Rod Hills
There be dragons...