Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?


Example: Search Files Using an Op Script

This sample script searches a file on a device running Junos OS for lines matching a given regular expression. The example uses the jcs:grep template in an op script.


This example uses a device running Junos OS.

Overview and Op Script

The jcs:grep template searches an ASCII file for lines matching a regular expression. The template resides in the junos.xsl import file, which is included with the standard Junos OS installation available on all switches, routers, and security devices running Junos OS. To use the jcs:grep template in a script, you must import the junos.xsl file into the script and map the jcs prefix to the namespace identified by the URI .

In this example, all values required for the jcs:grep template are defined as global parameters. The values for the parameters are passed into the script as command-line arguments. The following script defines two parameters, filename and pattern, which store the values of the input file path and the regular expression. If you omit either argument when you execute the script, the script generates an error and halts execution. Otherwise, the script calls the jcs:grep template and passes in the supplied arguments.

If the regular expression contains a syntax error, the jcs:grep template generates an error: regex error message for each line in the file. If the regular expression syntax is valid, the template parses the input file. For each match, the template adds a <match> element, which contains <input> and <output> child tags, to the result tree. The template writes the matching string to the <output> child element and writes the corresponding matching line to the <input> child element:

In the SLAX script, the := operator copies the results of the jcs:grep template call to a temporary variable and runs the node-set function on that variable. The := operator ensures that the results variable is a node-set rather than a result tree fragment so that the script can access the contents. The XSLT script explicitly calls out the equivalent steps. The script then loops through all resulting input elements and prints each match.

XSLT Syntax

SLAX Syntax



Step-by-Step Procedure

To download, enable, and run the script:

  1. Copy the XSLT or SLAX script into a text file, name the file grep.xsl or grep.slax as appropriate, and download it to the /var/db/scripts/op/ directory on the device.

  2. In configuration mode, include the file statement at the [edit system scripts op] hierarchy level and grep.xsl or grep.slax as appropriate.

  3. Issue the commit and-quit command to commit the configuration and to return to operational mode.

  4. Execute the op script by issuing the op grep filename filename pattern pattern operational mode command.


Verifying the Script Arguments


Verify that the argument names and descriptions appear in the command-line interface (CLI) help.


Issue the op grep ? operational mode command. The CLI lists the possible completions for the script arguments based on the definitions within the global variable arguments in the script.

Verifying Op Script Execution


Verify that the script behaves as expected.


If you issue the op grep command, but you fail to supply either the filename or the regex pattern, the script issues an error message and halts execution. For example:

When you issue the op grep filename filename pattern pattern command, the script lists all lines from the input file that match the regular expression.