Support Support Downloads Knowledge Base Service Request Manager My Juniper Community

Knowledge Base

Search our Knowledge Base sites to find answers to your questions.

Ask All Knowledge Base Sites All Knowledge Base Sites JunosE Defect (KA)Knowledge BaseSecurity AdvisoriesTechnical BulletinsTechnotes Sign in to display secure content and recently viewed articles

[Junos] How to use the 'replace pattern' command to replace multiple lines of configuration without having to delete and re-add

0

0

Article ID: KB11711 KB Last Updated: 03 Apr 2019Version: 6.0
Summary:

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 2.2.2.n with 1.1.1.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 address 2.2.2.x to 1.1.1.x within the system static-host-mapping hierarchy).
    [edit system]
    user@host# show static-host-mapping
    test1 inet 1.1.1.1;
    test2 inet 1.1.1.2;
    test3 inet 1.1.1.3;
    test4 inet 2.2.2.4;
    test5 inet 2.2.2.5;
  2. Issue the command: replace pattern <string1> with <string2> (enter). 
    [edit system]
    user@host# replace pattern 2.2.2. with 1.1.1.
  3. Verify the change using the 'show' command
    [edit system]
    user@host# show static-host-mapping
    test1 inet 1.1.1.1;
    test2 inet 1.1.1.2;
    test3 inet 1.1.1.3;
    test4 inet 1.1.1.4;
    test5 inet 1.1.1.5;
  4. Commit the changes.

 

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

replace pattern 1.1.1.1 with 2.2.2.2

If the pattern 1.1.1.10, 1.1.1.11 etc. exists in the configuration, the 'replace pattern' command  (shown above) will replace all at same time; resulting in the following changes:

1.1.1.1 will change to  2.2.2.2
1.1.1.1
0 will change to  2.2.2.20
1.1.1.11 will change to  2.2.2.21


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

replace pattern "1.1.1.1$" with 2.2.2.2

Will result in only 1.1.1.1 getting replaced with 2.2.2.2; all other variants of 1.1.1.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.
Comment on this article > Affected Products Browse the Knowledge Base for more articles related to these product categories. Select a category to begin.

Security Alerts and Vulnerabilities

Security Alerts and Vulnerabilities Product Alerts and Software Release Notices Problem Report (PR) Search Tool EOL Notices and Bulletins JTAC User Guide Customer Care User Guide Pathfinder SRX High Availability Configurator SRX VPN Configurator Training Courses and Videos End User Licence Agreement Global Search