Searching and Filtering Show Command Output

You can filter output from show commands to display lines containing a specified string, lines that do not contain a specified string, or output starting with a line containing a specified string. The search string is a regular expression consisting of a single character or a string of characters. You can use special characters to construct complex regular expressions.

Using Special Characters to Construct Complex Regular Expressions

Special characters allow you to construct complex regular expressions to filter output from show commands. You can use a regular expression to specify a single character or multiple characters as a search string. In addition, you can include special characters that influence the way the software matches the output against the search string. Supported special characters are listed in the following table.

Special Characters for Regular Expressions

Character

Operation

.

The period matches any single character, including a blank space.

For example, the following regular expression matches "aaz", "abz", "acz", and so on, but not just "az":

a.z

*

The asterisk matches zero or more sequential instances of a pattern.

For example, the following regular expression matches output that contains the string "abc", followed by zero or more Xs:

abcX*

+

The plus sign matches one or more sequential instances of a pattern.

For example, the following regular expression matches output that contains "de", followed by a sequence of "g"s, such as "deg", "degg", "deggg", and so on:

deg+

?

The question mark matches zero occurrences or one occurrence of a pattern.

For example, the following regular expression matches output that contains "dg" or "deg":

de?g

Note: Normally, when you type a question mark, the CLI lists the commands or options at that CLI level that begin with the character or string you entered. However, if you enter Ctrl+V and then type a question mark, the question mark is inserted into the command line, allowing you to use it as part of a regular expression.

^

A caret (when not used within brackets) matches the beginning of an input string.

For example, the following regular expression matches output that begins with "deg":

^deg

$

A dollar sign matches the end of an input string.

For example, the following regular expression matches output that ends with "deg":

deg$

_

An underscore matches one or more of the following:

  • , (comma)
  • { (left curly brace)
  • } (right curly brace)
  • ( (left parenthesis)
  • ) (right parenthesis)
  • The beginning of the input string
  • The end of the input string
  • A blank space

For example, the following regular expression matches "100" but not "1002", "2100", and so on.

_100_

[ ]

Square brackets enclose a range of single-character patterns.

For example, the following regular expression matches output that contains "1", "2", "3", "4", or "5":

[1-5]

You can use the following expression symbols within the brackets. These symbols are allowed only inside the brackets.

  • ^: A caret matches any characters except the ones in the brackets. For example, the following regular expression matches output that does not contain "1", "2", "3", "4", or "5":

    [^1-5]

  • -: A hyphen separates the beginning and ending of a range of characters. A match occurs if any of the characters within the range is present. Refer to the previous example.

|

A vertical bar separates two alternative values or sets of values. The output can match one or the other value.

For example, the following regular expression matches output that contains either "abc" or "defg":

abc|defg

( )

Parentheses allow you to create complex expressions.

For example, the following complex expression matches "abc", "abcabc", or "defg", but not "abcdefgdefg":

((abc)+)|((defg)?)

If you want to filter for a special character instead of using the special character as described in the preceding table, enter a backslash ( \ ) before the character. For example, to filter on output containing an asterisk, enter the asterisk portion of the regular expression as "\*".

device# show ip route bgp | include \*

Displaying Lines Containing a Specified String

Use the | include syntax with a regular expression to filter the output of any show commands that include the regular expression. In the following example, the output of the show interfaces tunnel command for tunnel 10 is filtered to display only lines containing the word "Internet". This command filters the output of the show interfaces tunnel command to display only lines that contain the word "Internet".

device# show interfaces tunnel 10 | include Internet
  Internet address is 10.11.1.1/31, MTU 1476 bytes, encapsulation GRE
Note: The vertical bar ( | ) is part of the command.

Note that the regular expression specified as the search string is case-sensitive. In the previous example, a search string of "Internet" would match the line containing the IP address, but a search string of "internet" would not.

Displaying Lines that Do Not Contain a Specified String

Use the | exclude syntax with a regular expression to filter the output of any show commands that do not match (exclude) the regular expression. In the following example, the output of the show who command is filtered to display only the lines that do not contain the word "closed". This command can be used to display open connections to the RUCKUS device.

device# show who | exclude closed
Console connections:
        established
        you are connecting to this session
        2 seconds in idle
Telnet connections (inbound):
 1      established, client ip address 10.168.9.37
        27 seconds in idle
Telnet connection (outbound):
SSH connections:

Displaying Lines Starting with a Specified String

Use the | begin syntax with a regular expression to filter the output of any show commands to display only output starting with the first line that starts with a regular expression match. In the following example, the output of the show who command is filtered to display output starting with the first line that contains the word "SSH". This command can be used to display information about SSH connections to the RUCKUS device.

device# show who | begin SSH
SSH connections:
 1      established, client ip address 10.168.9.210
        7 seconds in idle
 2      closed
 3      closed
 4      closed
 5      closed