- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Parsing a string
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 04:23 AM
11-19-2003 04:23 AM
" 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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 04:55 AM
11-19-2003 04:55 AM
Re: Parsing a string
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 05:06 AM
11-19-2003 05:06 AM
Re: Parsing a string
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 05:20 AM
11-19-2003 05:20 AM
Re: Parsing a string
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 05:34 AM
11-19-2003 05:34 AM
Re: Parsing a string
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..."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 05:51 AM
11-19-2003 05:51 AM
Re: Parsing a string
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 07:06 AM
11-19-2003 07:06 AM
Re: Parsing a string
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 07:11 AM
11-19-2003 07:11 AM
Re: Parsing a string
#!/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]);
}}}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 07:15 AM
11-19-2003 07:15 AM
Re: Parsing a string
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 07:20 AM
11-19-2003 07:20 AM
Re: Parsing a string
$ 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 07:38 AM
11-19-2003 07:38 AM
Re: Parsing a string
Thanks. It works. I had some other problem in the script.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 08:23 AM
11-19-2003 08:23 AM
Re: Parsing a string
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 09:13 AM
11-19-2003 09:13 AM
SolutionAre 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 10:33 AM
11-19-2003 10:33 AM
Re: Parsing a string
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 10:40 AM
11-19-2003 10:40 AM
Re: Parsing a string
Val2=`expr ${s1} : ".*v2.\([0-9]*\)"`
greetings,
Michael
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2003 05:27 PM
11-19-2003 05:27 PM
Re: Parsing a string
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2003 08:54 AM
11-20-2003 08:54 AM
Re: Parsing a string
Thank you all guys!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2003 09:47 AM
11-20-2003 09:47 AM
Re: Parsing a string
Can you share your solution with the rest of the forum?
Then we all benefit...
-- Rod Hills