in reply to How to escape parens for grep

The thing that starts with "tethereal " is a string (not a "command"). I think your post is a little confusing, because the string contains "6 or 17", but you say you want to assign "6,7", to a variable called $protocol.

Anyway, the way to set up a regex to capture the pieces you want involves ignoring or escaping the regex-special characters (the parens in this case) -- something like this would do:

$_ = "tethereal (proto 6 or 17) and src host suntest1 and dst host sun +test2 -w /tmp/dumpfile"; ( /\(proto ([^)]+)\) and src host (\S+) and dst host (\S+) -w (\S+)/ ) and ( $protocol, $srchost, $dsthost, $dir ) = ($1,$2,$3,$4); $protocol =~ s/ or /,/g;
(that is, if you want the value of $protocol to be a string consisting of comma-separated numbers, as opposed to something else)

Note the backslashes in front of the open and close parens that are intended to match actual parens in the string.

Replies are listed 'Best First'.
Re^2: How to escape parens for grep
by swaroop (Beadle) on Aug 15, 2005 at 01:44 UTC
    Hi graff,

    Thanks for the reply. But sometimes the filters does't exists. I mean we may run with out specifying the proto / srchost / dsthost /dir.

    So, we need some generic solution to extract this command.

    Thanks again.
    - Swaroop
      In that case you can split up the regex into multiple regex's and see if each one matches...
      my $s = "tethereal (proto 6 or 17) and src host suntest1 and dst host +suntest2 -w /tmp/dumpfile"; my %info; $info{srchost} = $1 if $s =~ /\bsrc host (\S+)/; $info{dsthost} = $1 if $s =~ /\bdst host (\S+)/; $info{dir} = $1 if $s =~ /\b-w (\S+)/; if( $s =~ /\(proto (.*?)\)/ ){ # this one needs extra processing my $prot = $1; # start with the proto strin +g: "6 or 17" my @prot = $prot =~ /(\d+)/g; # pull out the integers: + (6, 17) $info{protocol = join ',', @prot; # create the desired string: + '6,17' } # if necessary, validate %info here to make sure you can continue...