[ Contents] [ Prev] [ Next] [ Index] [ Report an Error]

Using Search and Replace

Modifying a configuration often requires you to search for an identifier or value and replace it with another. For example, suppose you change the IP address of the fe-0/0/1 interface and need to update it throughout the configuration.

The replace configuration mode command allows you to find and replace identifiers or values without manually searching the configuration hierarchy on the router or uploading the configuration file into a text editor. The replace command supports regular expressions, and allows you to perform find and replace operations using complex search criteria. For example, you can use regular expressions to find and replace all instances of IP addresses starting with 192.

The search-and-replace operation of the replace command is case-sensitive, and case-sensitivity cannot be disabled. In addition, the replace command does not support lazy quantifiers in regular expressions.

You can use the replace command at any level in the configuration hierarchy. Identifiers or values specified are replaced from that level downwards in the configuration hierarchy. For example, if you use the replace command at the [edit interfaces] hierarchy level to change an IP address from 10.1.1.1 to 10.2.1.1, the changes are made in the configuration hierarchy under interfaces, but not throughout the configuration.

To replace an identifier or value in the configuration, enter the replace command as follows:

replace pattern identifier 1 with identifier 2 upto number

The upto option does not specify the number of instances of the pattern to be replaced, but the number of configuration objects within which the search-and-replace operation is performed. For example, if you issue the replace command at the [edit interfaces] hierarchy level, and specify 3 for upto, the search-and-replace operation is performed within the first three configuration objects under interfaces—for example, fe-0/0/0, e1-5/0/1, and lo0.

If you do not specify the upto option, all occurrences of the specified pattern are replaced from the configuration hierarchy level downwards.

For more information, see the JUNOS CLI User Guide.

The examples in Table 15 illustrate ways in which you can use the replace command.

Table 15: Sample replace Commands

Sample Command

Result

[edit]
user@host# replace pattern 10.1.1.1 with 10.2.1.1

Replaces 10.1.1.1 with 10.2.1.1 throughout the configuration.

[edit interfaces fe-3/0/1 unit 0]
user@host# replace pattern 1bf5 with 1bf4

Replaces all the instances of the last byte of the IPv6 address 1bf5 with 1bf4 under [edit interfaces fe-3/0/1 unit 0].

[edit]
user@host# replace pattern “(.*):1bf5” with “\11bf5”

Replaces all instances of the IPv6 address 2000::c0a8::1bf5 with 2000::c0a8:1bf5.


[ Contents] [ Prev] [ Next] [ Index] [ Report an Error]