Troll said:
OK, I had some luck getting the first value incremented but no more.
Version which works:
*****************
if (/tcp/) {
my($Proto)=
/^(\w+)/;
$Protos{$Proto}++;
}
print "TCP = $Protos{'tcp'}\n";
#output section
TCP = 6 # all is correct here
Version which does not work:
**********************
if (/tcp/) {
my($Proto, $RecvQ)=
/^(\w+) (\s+)/;
The above re says find and extract a word into $Proto, find some
whitepsace and ignore it, then find more whitepsace and extract it into
$RecvQ. Do you mean to use an uypper-case S, meaning find non-whitepsace..?
$Protos{$Proto}++;
$RecvQs{$RecvQ)++;
}
print "TCP = $Protos{'tcp'}\n";
print "RecvQ = $RecvQs{'0'}\n";
#output section
TCP = 6 # all is correct here
Use of uninitialized value in concatenation (.) or string at... # error
time - this refers to the 2nd print statement
RecvQ = # this is blank
I have tried reading the second parameter as a (\s+) and as a (\d+) with no
luck. If you run netstat you will probably see that all items in the RecvQ
column are 0.
What have I done wrong now?
If you are trying to parse this:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 redhat:ssh winxp:1099 ESTABLISHED
how about this:
my ($proto,$rxQ,$txQ,$l_addr,$r_addr,$state) =
/^(\w+) (\d+) (\d+) (\S+) (\S+) (\w+)/;
which says (with whitespace in between each):
find a word and extract into $proto,
find a number and extract into $rxQ,
ditto for $txQ,
find NON-whitespace and extract into $l_addr,
ditto for $r_addr,
find a word and extract into $state.
Use \S+ for the addresses because they contain numbers, letters, and a
colon. Neither \w nor \d would match these.
Can a number of whitespaces be represented by:
/^(\w+) (\s+)/; # this is a word followed by some spaces followed by a
string
or is the above only ONE whitespace?
\s (lower-case) DOES NOT mean a string, it means whitespace.
\S (upper-case) means non-whitespace.
If you have access to "The Camel Book" by ORA, try reading the section
on pattern matching. It's clear you're not getting how to construct a
meaningful regular expression.