8-35
Cisco Content Services Switch Administration Guide
OL-5647-02
Chapter 8 Using the CSS Scripting Language
Using the grep Command
Specifying Line Numbers for Search Results
The search results from a
grep
command can produce multiple lines. In this case,
you can specify the line number you want to set in the UGREP variable by issuing
grep
-u
[n]
, where
[n]
is an optional line number. By default, the last line of the
search results is saved in the UGREP variable. Suppose that you want to issue a
grep
command on the
show service
command for the service S1 and search for
all the colon (:) characters in the
show service
screen. For example, enter:
!no echo
show service S1 | grep -u “:”
echo “The line is: ${UGREP}”
The output is:
The line is: Weight: 1 Load: 255
By default, this is the last line in the
show service
screen that satisfies the search
criteria, but it is not the only line that qualifies. To search for a specific line, for
example the first line that satisfies the search criteria, use the
[n]
option. For
example, enter:
!no echo
show service S1 | grep -u1 “:”
echo “The line is: ${UGREP}”
The output is:
The line is: Name: S1 Index: 1
This is the first line that satisfies the search criteria. Notice the -u1 option, which
tells the script to search for the colon character (:) and set the UGREP variable
equal to the first qualified search result.
STATUS Results from the grep Command
The STATUS code result from the
grep
command equals the number of qualified
search results. This is important to note because other script commands return a
zero result to indicate success. The grep command returns just the opposite. If it
finds 14 matches, then the STATUS variable is set to 14. If it finds no matches,
then the STATUS variable is set to 0.