To make global changes to variables and identifiers in a configuration, use the replace configuration mode command. This command replaces a pattern in a configuration with another pattern. For example, you can use this command to find and replace all occurrences of an interface name when a PIC is moved to another slot in the router.
- user@host# replace pattern pattern1 with pattern2 <upto n>
pattern pattern1 is a text string or regular expression that defines the identifiers and values you want to replace in the configuration.
pattern2 is a text string or regular expression that replaces the identifiers and values located with pattern1.
Juniper Networks uses standard UNIX-style regular expression syntax (as defined in POSIX 1003.2). If the regular expression contains spaces, operators, or wildcard characters, enclose the expression in quotation marks. Greedy qualifiers (match as much as possible) are supported. Lazy qualifiers (match as little as possible) are not.
The upto n option specifies the number of objects replaced. The value of n controls the total number of objects that are replaced in the configuration (not the total number of times the pattern occurs). Objects at the same hierarchy level (siblings) are replaced first. Multiple occurrences of a pattern within a given object are considered a single replacement. For example, if a configuration contains a 010101 text string, the command
- replace pattern 01 with pattern 02 upto 2
replaces 010101 with 020202 (instead of 020201). Replacement of 010101 with 020202 is considered a single replacement (n = 1), not three separate replacements (n =3).
If you do not specify an upto option, all identifiers and values in the configuration that match pattern1 are replaced.
The replace command is available in configuration mode at any hierarchy level. All matches are case-sensitive.
Table 19 shows some common regular expressions you can use with the replace command. Table 20 provides some examples of pattern replacement.
Table 19: Common Regular Expressions to Use with replace Command
Table 20: Replacement Examples
Replace an interface name in a configuration:
- [edit]
- user@host# show
- protocols {
-
- ospf {
-
- area 0.0.0.0 {
-
- interface so-0/0/0 {
- hello-interval 5;
- }
- }
- }
- }
- [edit]
- user@host# replace so-0/0/0 with so-1/1/0
- [edit]
- user@host# show
- protocols {
-
- ospf {
-
- area 0.0.0.0 {
-
- interface so-1/1/0 {
- hello-interval 5;
- }
- }
- }
- }
Use the \n back reference to replace a pattern:
- [edit]
- user@host# show interfaces
- xe-0/0/0 {
- unit 0;
- }
- fe-3/0/1 {
- vlan-tagging;
-
- unit 0 {
- description "inet6 configuration. IP: 2000::c0a8::1bf5";
- vlan-id 100;
-
- family inet {
- address 17.10.1.1/24;
- }
-
- family inet6 {
- address 2000::c0a8:1bf5/3;
- }
- }
- }
- [edit]
- user@host# replace pattern "(.*):1bf5" with
"\11bf5"
- [edit]
- user@host# show interfaces
- xe-0/0/0 {
- unit 0;
- }
- fe-3/0/1 {
- vlan-tagging;
-
- unit 0 {
- description "inet6 configuration. IP: 2000::c0a8:1bf5";
- vlan-id 100;
-
- family inet {
- address 17.10.1.1/24;
- }
-
- family inet6 {
- address 2000::c0a8:1bf4/3;
- }
- }
- }
The pattern 2000::c0a8::1bf5 is replaced with 2000::c0a8:1bf5.
Consider the hierarchy shown in Figure 22. The text string 010101 appears in three places (description sections of xe-0/0/0, xe-0/0/0.0, and fe-0/0/1). These three instances are three objects.
Use the upto option to perform a replacement:
- user@host# show interfaces
- xe-0/0/0 {
- description "mkt 010101"; #1st instance in the hierarchy
-
- unit 0 {
- description "mkt 010101"; #3rd instance in the hierarchy
(child of the 1st
- instance)
- }
- }
- fe-0/0/1 {
- description "mkt 010101"; #2nd instance in the hierarchy
(sibling of the 1st
- instance)
-
- unit 0 {
-
- family inet {
- address 200.200.20.2/24;
- }
- }
- }
- [edit]
- user@host# replace pattern 01 with 02 upto
2
- [edit]
- user@host# commit
- commit complete
An upto 2 option in the replace command converts 01 to 02 for two object instances. The objects under the main interfaces xe-0/0/0 and fe-0/0/1 will be replaced first (since these are siblings in the hierarchy level). Because of the upto 2 restriction, the replace command replaces patterns in the first and second instance in the hierarchy (siblings), but not the third instance (child of the first instance).
- [edit]
- user@host# show interfaces
- xe-0/0/0 {
- description "mkt 020202"; #1st instance in the hierarchy
-
- unit 0 {
- description "mkt 010101"; #3rd instance in the hierarchy
(child of the 1st
- instance)
- }
- }
- fe-0/0/1 {
- description "mkt 020202"; #2nd instance in the hierarchy
(sibling of the 1st
- instance)
-
- unit 0 {
-
- family inet {
- address 200.200.20.2/24;
- }
- }
- }
Figure 22: Replacement by Object
