how to extract data from optional patterns?

S

sam

Hi,

I need to parse some strings that is seperated by the "=" character
shown as below:

<message type=’Logon’ session=’SN’ status=’Status’ err_msg=’ErrMsg’/>

<message sn = ’SN’ msg_type = ’Msg’ date = ’Date’ time = ’Time’ host =
’IP’ app=’App’ severity = 'Severity' msg = 'Msg'/>

<message> <request = 'Logon' username = 'someone' password = 'pass'/>

<message session_number='SessionNumber' request='Logoff'/>



I want to extract the information my_string from
the pattern ='(my_string)'

The following is the perl script with pattern
#!/usr/bin/perl -w
use strict;
while (<DATA>) {
print "$1 $2 $3\n" if /^<message.+= ?'([^=^']+)' *[A-Za-z]+ ?=
?'([^=^']+)' *(.*)>$/; } __DATA__
<message> <request = 'Logon' username = 'someone' password = 'pass'/>

The result is:
sec@fbsd [3:52pm] [~/unpv12e/s_ipmon]> !p
perl reg_test4.pl
someone pass /

But my expect result is:
Logon someone pass

Any suggestion is very appreciated.
Thanks
Sam
 
J

John Bokma

sam said:
Hi,

I need to parse some strings that is seperated by the "=" character
shown as below:

<message type=’Logon’ session=’SN’ status=’Status’ err_msg=’ErrMsg’/>

<message sn = ’SN’ msg_type = ’Msg’ date = ’Date’ time = ’Time’ host =
’IP’ app=’App’ severity = 'Severity' msg = 'Msg'/>

<message> <request = 'Logon' username = 'someone' password = 'pass'/>

<message session_number='SessionNumber' request='Logoff'/>



I want to extract the information my_string from
the pattern ='(my_string)'
XML?

The following is the perl script with pattern
#!/usr/bin/perl -w

remove -w and use

use warnings;
use strict;
while (<DATA>) {
print "$1 $2 $3\n" if /^<message.+= ?'([^=^']+)' *[A-Za-z]+ ?=
?'([^=^']+)' *(.*)>$/; } __DATA__
<message> <request = 'Logon' username = 'someone' password = 'pass'/>

Read about non-greedy matching, but if you really want to do something
with XML data, use a module that can parse XML.
The result is:
sec@fbsd [3:52pm] [~/unpv12e/s_ipmon]> !p
perl reg_test4.pl
someone pass /

Note: [^=^'] no need to put in the second ^ (unless you don't want to
match ^)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top