Searching and Filtering CLI Output
The Cisco IOS CLI provides ways of searching through large amounts of command output and filtering output to exclude information you do not need. These features are enabled forshow and more commands, which generally display large amounts of data.
Note | Show and more commands are always entered in user EXEC or privileged EXEC. |
When output continues beyond what is displayed on your screen, the Cisco IOS CLI displays a --More-- prompt. Pressing Return displays the next line; pressing the Spacebar displays the next screen of output. The CLI String Search feature allows you to search or filter output from --More-- prompts.
- Finding Feature Information
- Understanding Regular Expressions
- Searching and Filtering CLI Output Examples
Finding Feature Information
Use Cisco Feature Navigator to find information about platform support and Cisco IOS and Catalyst OS software image support. To access Cisco Feature Navigator, go to http://www.cisco.com/go/cfn . An account on Cisco.com is not required.
Understanding Regular Expressions
A regular expression is a pattern (a phrase, number, or more complex pattern) the CLI String Search feature matches against show or more command output. Regular expressions are case-sensitive and allow for complex matching requirements. Simple regular expressions include entries like Serial, misses, or 138. Complex regular expressions include entries like 00210... , ( is ), or [Oo]utput.
A regular expression can be a single-character pattern or a multiple-character pattern. That is, a regular expression can be a single character that matches the same single character in the command output or multiple characters that match the same multiple characters in the command output. The pattern in the command output is referred to as a string. This section describes creating both single-character patterns and multiple-character patterns. It also discusses creating more complex regular expressions using multipliers, alternation, anchoring, and parentheses.
- Single-Character Patterns
- Searching and Filtering show Commands
- Searching and Filtering more Commands
- Searching and Filtering from the --More--Prompt
Single-Character Patterns
The simplest regular expression is a single character that matches the same single character in the command output. You can use any letter (A-Z, a-z) or digit (0-9) as a single-character pattern. You can also use other keyboard characters (such as ! or ~) as single-character patterns, but certain keyboard characters have special meaning when used in regular expressions. The table below lists the keyboard characters that have special meaning.
Character |
Special Meaning |
---|---|
. |
Matches any single character, including white space. |
* |
Matchers 0 or more sequences of the pattern. |
+ |
Matches 1 or more sequences of the pattern. |
? |
Matches 0 or 1 occurrences of the pattern. |
^ |
Matches the beginning of the string. |
$ |
Matches the end of the string. |
_ (underscore) |
Matches a comma (,), left brace ({), right brace (}), left parenthesis ( ( ), right parenthesis ( ) ), the beginning of the string, the end of the string, or a space. |
To use these special characters as single-character patterns, remove the special meaning by preceding each character with a backslash (\). The following examples are single-character patterns matching a dollar sign, an underscore, and a plus sign, respectively.
\$ \_ \+
You can specify a range of single-character patterns to match against command output. For example, you can create a regular expression that matches a string containing one of the following letters: a, e, i, o, or u. Only one of these characters must exist in the string for pattern matching to succeed. To specify a range of single-character patterns, enclose the single-character patterns in square brackets ([]). For example, [aeiou]matches any one of the five vowels of the lowercase alphabet, while [abcdABCD] matches any one of the first four letters of the lower- or uppercase alphabet.
You can simplify ranges by entering only the endpoints of the range separated by a dash (-). Simplify the previous range as follows:
[a-dA-D]
To add a dash as a single-character pattern in your range, include another dash and precede it with a backslash:
[a-dA-D\-]
You can also include a right square bracket (]) as a single-character pattern in your range, as shown here:
[a-dA-D\-\]]
The previous example matches any one of the first four letters of the lower- or uppercase alphabet, a dash, or a right square bracket.
You can reverse the matching of the range by including a caret (^) at the start of the range. The following example matches any letter except the ones listed:
[^a-dqsv]
The following example matches anything except a right square bracket (]) or the letter d:
[^\]d]
Multiple-Character Patterns
When creating regular expressions, you can also specify a pattern containing multiple characters. You create multiple-character regular expressions by joining letters, digits, or keyboard characters that do not have special meaning. For example, a4% is a multiple-character regular expression. Insert a backslash before the keyboard characters that have special meaning when you want to indicate that the character should be interpreted literally.
With multiple-character patterns, order is important. The regular expression a4% matches the character a followed by a 4 followed by a % sign. If the string does not have a4%, in that order, pattern matching fails. The multiple-character regular expression a.uses the special meaning of the period character to match the letter a followed by any single character. With this example, the strings ab, a!, or a2 are all valid matches for the regular expression.
You can remove the special meaning of the period character by inserting a backslash before it. For example, when the expression a\. is used in the command syntax, only the string a. will be matched.
You can create a multiple-character regular expression containing all letters, all digits, all keyboard characters, or a combination of letters, digits, and other keyboard characters. For example, telebit3107v32bis is a valid regular expression.
Multipliers
You can create more complex regular expressions that instruct Cisco IOS software to match multiple occurrences of a specified regular expression. To do so, you use some special characters with your single-character and multiple-character patterns. The table below lists the special characters that specify “multiples” of a regular expression.
Character |
Description |
---|---|
* |
Matches 0 or more single-character or multiple-character patterns. |
+ |
Matches 1 or more single-character or multiple-character patterns. |
? |
Matches 0 or 1 occurrences of a single-character or multiple-character pattern. |
The following example matches any number of occurrences of the letter a, including none:
a*
The following pattern requires that at least one letter a be in the string to be matched:
a+
The following pattern matches the string bb or bab:
ba?b
The following string matches any number of asterisks (*):
\**
To use multipliers with multiple-character patterns, you enclose the pattern in parentheses. In the following example, the pattern matches any number of the multiple-character string ab:
(ab)*
As a more complex example, the following pattern matches one or more instances of alphanumeric pairs, but not none (that is, an empty string is not a match):
([A-Za-z][0-9])+
The order for matches using multipliers (*, +, or ?) is to put the longest construct first. Nested constructs are matched from outside to inside. Concatenated constructs are matched beginning at the left side of the construct. Thus, the regular expression matches A9b3, but not 9Ab3 because the letters are specified before the numbers.
Alternation
Alternation allows you to specify alternative patterns to match against a string. You separate the alternative patterns with a vertical bar (|). Exactly one of the alternatives can match the string. For example, the regular expression codex|telebit matches the string codex or the string telebit, but not both codex and telebit.
Anchoring
You can instruct Cisco IOS software to match a regular expression pattern against the beginning or the end of the string. That is, you can specify that the beginning or end of a string contain a specific pattern. You “anchor” these regular expressions to a portion of the string using the special characters shown in the table below.
Character |
Description |
---|---|
^ |
Matches the beginning of the string. |
$ |
Matches the end of the string. |
For example, the regular expression ^conmatches any string that starts with con, and $sole matches any string that ends with sole.
In addition to indicating the beginning of a string, the ^ symbol can be used to indicate the logical function “not” when used in a bracketed range. For example, the expression [^abcd]indicates a range that matches any single letter, as long as it is not the letters a, b, c, or d.
Contrast these anchoring characters with the special character underscore (_). Underscore matches the beginning of a string (^), the end of a string ($), parentheses (( )), space ( ), braces ({}), comma (,), or underscore (_). With the underscore character, you can specify that a pattern exist anywhere in the string. For example, _1300_matches any string that has 1300 somewhere in the string. The string 1300 can be preceded by or end with a space, brace, comma, or underscore. So, although {1300_matches the regular expression _1300_, 21300 and 13000 do not.
Using the underscore character, you can replace long regular expression lists. For example, instead of specifying ^1300()()1300${1300,,1300,{1300},1300,(1300you can specify simply _1300_.
Parentheses for Recall
As shown in the “Multipliers” section, you use parentheses with multiple-character regular expressions to multiply the occurrence of a pattern. You can also use parentheses around a single- or multiple-character pattern to instruct the Cisco IOS software to remember a pattern for use elsewhere in the regular expression.
To create a regular expression that recalls a previous pattern, you use parentheses to indicate memory of a specific pattern and a backslash (\) followed by a number to reuse the remembered pattern. The number specifies the occurrence of a parentheses in the regular expression pattern. If you have more than one remembered pattern in your regular expression, then \1 indicates the first remembered pattern, and \2 indicates the second remembered pattern, and so on.
The following regular expression uses parentheses for recall:
a(.)bc(.)\1\2
This regular expression matches an a followed by any character (call it character no. 1), followed by bc followed by any character (character number 2), followed by character no. 1 again, followed by character number. 2 again. So, the regular expression can match aZbcTZT. The software remembers that character number 1 is Z and character number 2 is T and then uses Z and T again later in the regular expression.
Searching and Filtering show Commands
To searc h show command output, use the following command in privileged EXEC mode:
Command |
Purpose |
---|---|
Router# show any-command | begin regular-expression |
Begins unfiltered output of the show command with the first line that contains the regular expression. |
Note | Cisco IOS documentation generally uses the vertical bar to indicate a choice of syntax. However, to search the output of show and more commands, you will need to enter the pipe character (the vertical bar). In this section the pipe appears in bold ( | ) to indicate that you should enter this character. |
To filter show command output, use one of the following commands in privileged EXEC mode:
Command |
Purpose |
---|---|
Router# show any-command | exclude regular-expression |
Displays output lines that do not contain the regular expression. |
Router# show any-command | include regular-expression |
Displays output lines that contain the regular expression. |
On most systems you can enter the Ctrl-Z key combination at any time to interrupt the output and return to privileged EXEC mode. For example, you can enter the showrunning-config|beginhostname command to start the display of the running configuration file at the line containing the hostname setting, then use Ctrl-Z when you get to the end of the information you are interested in.
Note | Characters followed by an exclamation mark (!) or a semicolon (;) are considered as a comment and hence they are ignored in a command. |
Searching and Filtering more Commands
You can search more commands the same way you search show commands (more commands perform the same function as show commands). To search more command output, use the following command in user EXEC mode:
Command |
Purpose |
---|---|
Router# more any-command | begin regular-expression |
Begins unfiltered output of a more command with the first line that contains the regular expression. |
You can filter more commands the same way you filter show commands. To filter more command output, use one of the following commands in user EXEC mode:
Command |
Purpose |
---|---|
Router# more any-command | exclude regular-expression |
Displays output lines that do not contain the regular expression. |
Router# more any-command | include regular-expression |
Displays output lines that contain the regular expression. |
Searching and Filtering from the --More--Prompt
You can search output from --More-- prompts. To search show or more command output from a --More-- prompt, use the following command in user EXEC mode:
Command |
Purpose |
---|---|
-More-
/
regular-expression
|
Begins unfiltered output with the first line that contains the regular expression. |
You can filter output from --More-- prompts. However, you can specify only one filter for each command. The filter remains until the show or more command output finishes or until you interrupt the output (usingCtrl-Z or Ctrl-6). Therefore, you cannot add a second filter at a --More-- prompt if you already specified a filter at the original command or at a previous --More--prompt.
Note | Searching and filtering are different functions. You can search command output using the begin keyword and specify a filter at the --More-- prompt for the same command. |
To filter show or more command output at a --More-- prompt, use one of the following commands in user EXEC mode:
Command |
Purpose |
---|---|
-More-
-
regular-expression
|
Displays output lines that do not contain the regular expression. |
-More-
+
regular-expression
|
Displays output lines that contain the regular expression. |
Searching and Filtering CLI Output Examples
The following is partial sample output from the morenvram:startup-config|begin privileged EXEC mode command that begins unfiltered output with the first line that contains the regular expression ip. At the --More-- prompt, the user specifies a filter to exclude output lines that contain the regular expression ip.
Router# more nvram:startup-config | begin ip ip subnet-zero ip domain-name cisco.com ip name-server 192.168.48.48 ip name-server 172.16.2.132 ! isdn switch-type primary-5ess . . . interface Ethernet1 ip address 10.5.5.99 10.255.255.0 --More-- -ip filtering... media-type 10BaseT ! interface Serial0:23 encapsulation frame-relay no keepalive dialer string 4001 dialer-group 1 isdn switch-type primary-5ess no fair-queue
The following is partial sample output of the morenvram:startup-config|include command. It only displays lines that contain the regular expression ip.
Router# more nvram:startup-config | include ip ip subnet-zero ip domain-name cisco.com ip name-server 1192.168.48.48 ip name-server 172.16.2.132
The following is partial sample output from the morenvram:startup-config|excludecommand. It excludes lines that contain the regular expression service. At the --More-- prompt, the user specifies a filter with the regular expression Dialer1. Specifying this filter resumes the output with the first line that contains Dialer1.
Router# more nvram:startup-config | exclude service ! version 12.2 ! hostname router ! boot system flash no logging buffered ! ip subnet-zero ip domain-name cisco.com . . . --More-- /Dialer1 filtering... interface Dialer1 no ip address no ip directed-broadcast dialer in-band no cdp enable
The following is partial sample output from theshowinterface command with an output search specified. The use of the keywords beginEthernet after the pipe begins unfiltered output with the first line that contains the regular expression Ethernet. At the --More-- prompt, the user specifies a filter that displays only the lines that contain the regular expression Serial.
Router# show interface | begin Ethernet Ethernet0 is up, line protocol is up Hardware is Lance, address is 0060.837c.6399 (bia 0060.837c.6399) Description: ip address is 172.1.2.14 255.255.255.0 Internet address is 172.1.2.14/24 . . . 0 lost carrier, 0 no carrier 0 output buffer failures, 0 output buffers swapped out --More-- +Serial filtering... Serial1 is up, line protocol is up Serial2 is up, line protocol is up Serial3 is up, line protocol is down Serial4 is down, line protocol is down Serial5 is up, line protocol is up Serial6 is up, line protocol is up Serial7 is up, line protocol is up
The following is partial sample output from theshowbuffers|excludecommand. It excludes lines that contain the regular expression ip. At the --More-- prompt, the user specifies a search that continues the filtered output beginning with the first line that contains Serial0.
Router# show buffers | exclude 0 misses Buffer elements: 398 in free list (500 max allowed) Public buffer pools: Small buffers, 104 bytes (total 50, permanent 50): 50 in free list (20 min, 150 max allowed) 551 hits, 3 misses, 0 trims, 0 created Big buffers, 1524 bytes (total 50, permanent 50): 49 in free list (5 min, 150 max allowed) Very Big buffers, 4520 bytes (total 10, permanent 10): . . . Huge buffers, 18024 bytes (total 0 permanent 0): 0 in free list (0 min, 4 max allowed) --More-- /Serial0 filtering... Serial0 buffers, 1543 bytes (total 64, permanent 64): 16 in free list (0 min, 64 max allowed) 48 hits, 0 fallbacks
The following is partial sample output from theshowinterface|includecommand. The use of the include(is) keywords after the pipe (|) causes the command to display only lines that contain the regular expression ( is ). The parenthesis force the inclusion of the spaces before and after is. Use of the parenthesis ensures that only lines containing is with a space both before and after it will be included in the output (excluding from the search, for example, words like “disconnect”).
router# show interface | include ( is ) ATM0 is administratively down, line protocol is down Hardware is ATMizer BX-50 Dialer1 is up (spoofing), line protocol is up (spoofing) Hardware is Unknown DTR is pulsed for 1 seconds on reset Ethernet0 is up, line protocol is up Hardware is Lance, address is 0060.837c.6399 (bia 0060.837c.6399) Internet address is 172.21.53.199/24 Ethernet1 is up, line protocol is up Hardware is Lance, address is 0060.837c.639c (bia 0060.837c.639c) Internet address is 10.5.5.99/24 Serial0:0 is down, line protocol is down Hardware is DSX1 . . . --More--
At the --More-- prompt, the user specifies a search that continues the filtered output beginning with the first line that contains Serial0:13:
/Serial0:13 filtering... Serial0:13 is down, line protocol is down Hardware is DSX1 Internet address is 10.0.0.2/8 0 output errors, 0 collisions, 2 interface resets Timeslot(s) Used:14, Transmitter delay is 0 flag