Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Regexp

Status
Not open for further replies.

treid71

Technical User
May 12, 2009
3
0
0
Hi all,

I'm having a bit of a problem with a regexp in a tcl script I'm *attempting* to write for my Cisco router.

Long story short, I'm trying to compare the numerical value of the output against a threshold/variable I have set and the script just doesn't want to work. Here is the error I am receiving:

Register event failed:missing close-brace
while compiling
"foreach line [split $output "\n"]"


Here is the section of the script I am having a problem with. I want to extract the "-80" portion of the output and compare it (>=) against the threshold I have set of -75. Many thanks in advance, I have been pulling my hair out on this.


# Setup commands
set command "show cellular 0/2/0 radio | inc RSSI"
# Sample output of above: Current RSSI(RSCP) = -80 dBm


# Pull RSSI stats from router via CLI
set output [run_cli [list $command]]

# Pull out dBm with a regular expression
foreach line [split $output "\n"] {
if { [regexp {.*Current RSSI.* = (.*?)\} $line -> currentdBm] }
if { $currentdBm >= $rssi_signal_threshold }

action_syslog msg "Current RSSI on Cellular 0/2/0 is $currentdBm and has exceeded $rssi_signal_threshold"
}
 
Something's wrong for sure. The error statement implies an unmatched bracket (could be parentheses, square brackets, or braces). It doesn't appear to be a problem on the line noted, foreach line [split $output "\n"] so I'm inclined to suspect that it's an artifact of some previous line. That said, however, I can't make sense of what you have between the {}s of the foreach block.

The foreach structure takes each line of $output successively and applies it to the next three statements. So far so good.

Now, if { [regexp {.*Current RSSI.* = (.*?)\} $line -> currentdBm] } seems to be an if clause with no then. That is, the syntax should be if {hypothetical clause} {conclusion clause}. You have no conclusion.

Moreover, the regexp statement doesn't make sense to me either: [regexp {.*Current RSSI.* = (.*?)\} $line -> currentdBm]. The regexp syntax is regexp <regular expression> <string to match> <match variable> <group variable(s)>. I don't recognize -> as valid Tcl syntax.

_________________
Bob Rashkin
 
Hi mikrom,

I would like a regexp to extract the number that is in the following string.

Current RSSI(RSCP) = -80 dBm

In this case it is "-80" but it could change depending on the output.

Then I'd like to compare the extracted number to another number (a variable I have set) to see if it is greater than or equal to.



Thanks in advance!

 
Something like this
Code:
[COLOR=#804040][b]set[/b][/color] mystring [COLOR=#ff00ff]"Current RSSI(RSCP) = -80 dBm"[/color]
[COLOR=#804040][b]set[/b][/color] result [[COLOR=#804040][b]regexp[/b][/color] {Current RSSI\s*.*=\s*(-{[COLOR=#ff00ff]0[/color],[COLOR=#ff00ff]1[/color]}\s*\d+)} [COLOR=#008080]$mystring[/color] match number];

[COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$result[/color]} {
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"String to apply the pattern: '$mystring'"[/color]
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"result=$result"[/color]
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"This was matched: $match"[/color]
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Number (extracted): $number"[/color]
} [COLOR=#804040][b]else[/b][/color] {
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Nothing matched !"[/color]
}
Output:
Code:
C:\Users\Roman\Work>tclsh85 regex.tcl
String to apply the pattern: 'Current RSSI(RSCP) = -80 dBm'
result=1
This was matched: Current RSSI(RSCP) = -80
Number (extracted): -80
 
mikrom - that's exactly what I was looking for. Appreciate the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top