Description

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.

Solution

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 <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:

  1. Go into the directory of the tree that you want to make the change (in this example we want to change the address 192.0.2.x to 198.51.100.x within the system static-host-mapping hierarchy).
    [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 ;
  2. Issue the command: replace pattern <string1> with <string2> (enter). 
    [edit system]
    user@host# replace pattern 192.0.2. with 198.51.100.
  3. Verify the change using the 'show' command
    [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 ;
  4. Commit the changes.

 

Example 2:  To replace IP address 198.51.100.1 with 192.0.2.2 throughout the configuration, run the command as shown below.  

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


Example 3: To replace exact or specific configuration content using 'replace pattern' without impacting all variations of the pattern (such as 198.51.100.10, 198.51.100.11 etc.), use the pattern shown below.  The ' $ ' means match the end of the input string.

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.


Running this command at the top of the hierarchy will apply the changes to the complete configuration file. 

For more details, consult Modifying the Configuration for a Device - Using Global Replace in the Junos OS Configuration - Tech Library

Modification History

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 RFC5737
2024-12-17: Added SRX Series to categories.