Joshua Scott wrote:
>
> How can I split the data in a line by a single whitespace but also keep
> portions between quotes together?  Here is an example of a line of data I'm
> trying to parse:
>
> id=firewall time="2003-10-30 04:15:01" fw=66.74.67.229 pri=5 c=256 m=38
> msg="ICMP packet dropped" n=63211 src=1.1.1.1 dst=2.2.2.2
>
> What I would like to do is keep the data between the quotes together despite
> the spaces.  This is what I'm expecting to get:
>
> Id=firewall
> Time="2003-10-30 04:15:01"
> Fw=66.74.67.229
> Msg="ICMP packet dropped"
> ...etc
>
> How should I go about doing this?  What I have currently is a regex that
> splits out the entire line, but certain fields have changed so my original
> code doesn't work as well.

Hi Joshua.

The 'delimited string' FAQ is really for comma-separated values rather
than the 'name=value' format you have here. It also supports escaped
quotes within quoted strings, which you probably don't have. The
regex below should do what you want.

HTH,

Rob


use strict;
use warnings;

my $string = q{id=firewall time="2003-10-30 04:15:01" fw=66.74.67.229 pri=5 c=256 m=38 
msg="ICMP packet dropped" n=63211 src=1.1.1.1
dst=2.2.2.2};

my @fields = $string =~ m/\w+=(?:"[^"]+"|\S+)/g;

print map "$_\n", @fields;

*OUTPUT*

id=firewall
time="2003-10-30 04:15:01"
fw=66.74.67.229
pri=5
c=256
m=38
msg="ICMP packet dropped"
n=63211
src=1.1.1.1
dst=2.2.2.2



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to