This article explains how to use the 'replace pattern' command to replace multiple lines of configuration without having to delete and re-add. This is similar to 'find and replace' used in editors. The procedure is useful to quickly find and replace an IP address, policy action, or any other configuration to a new value.
In order to replace a specific pattern in the configuration of a Junos OS device, run the ' replace pattern ' command in edit mode
replace pattern
replace pattern <pattern> with <new_value>
NOTE: Since this is a regular expression, the command will replace all instances of the pattern. Example 1: Perform the following steps to replace IP address 192.0.2.n with 198.51.100.n in the system static-host-mapping hierarchy example below:
192.0.2.x to 198.51.100.x
[edit system] user@host# show static-host-mapping test1 inet 198.51.100.1; test2 inet 198.51.100.2; test3 inet 198.51.100.3; test4 inet 192.0.2.4 ; test5 inet 192.0.2.5 ;
replace pattern <string1> with <string2>
[edit system] user@host# replace pattern 192.0.2. with 198.51.100.
'show'
[edit system] user@host# show static-host-mapping test1 inet 198.51.100.1; test2 inet 198.51.100.2; test3 inet 198.51.100.3; test4 inet 198.51.100.4 ; test5 inet 198.51.100.5 ;
replace pattern 198.51.100.1 with 192.0.2.2 If the pattern 198.51.100.10 , 198.51.100.11 etc. exists in the configuration, the 'replace pattern' command (shown above) will replace all at same time; resulting in the following changes: 198.51.100.1 will change to 19 2 .0.2.2 198.51.100.1 0 will change to 192.0.2.2 0 198.51.100.1 1 will change to 192.0.2.2 1
replace pattern 198.51.100.1 with 192.0.2.2
If the pattern 198.51.100.10 , 198.51.100.11 etc. exists in the configuration, the 'replace pattern' command (shown above) will replace all at same time; resulting in the following changes: 198.51.100.1 will change to 19 2 .0.2.2 198.51.100.1 0 will change to 192.0.2.2 0 198.51.100.1 1 will change to 192.0.2.2 1
198.51.100.10
198.51.100.11
198.51.100.1
2 .0.2.2 198.51.100.1
0
192.0.2.2
1
'replace pattern'
replace pattern "198.51.100.1 $ " with 192.0.2.2 Will result in only 198.51.100.1 getting replaced with 19 2.0.2.2 ; all other variants of 198.51.100.1n will remain intact.
replace pattern "198.51.100.1 $ " with 192.0.2.2
2.0.2.2
198.51.100.1n
2019-04-03: Added additional examples.2021-05-25: Article reviewed for accuracy. No changes made. Article is correct and complete.2022-02-24: IPs changed per RFC57372024-12-17: Added SRX Series to categories.