Below is extract from the file to be changed: May use awk or sed...any utility is good for pattern matching.

rhts-mgr hostname                       g05-rhts01
rhts-mgr ntp-server-ip-addr             10.113.XX.31
rhts-mgr ntp-redundant-server-ip-addr   10.113.XX.32
rhts-mgr extern-iface ip-addr           10.113.XX.129
rhts-mgr extern-iface broadcast-addr    10.113.XX.255
rhts-mgr extern-iface default-gw        10.113.XX.1

I have tried variations of awk ,sed combinations but I am still new to this. I am trying to change file entries as above to below

rhts-mgr hostname                       g08-rhts01
rhts-mgr ntp-server-ip-addr   
rhts-mgr ntp-redundant-server-ip-addr
rhts-mgr extern-iface ip-addr 
rhts-mgr extern-iface broadcast-addr
rhts-mgr extern-iface default-gw

The logic is to :

  1. search rhts-mgr hostname in the and replace anyhostname entry in $3 from g*-rhts* to g08-rhts*.
  2. replace subnet in ip address in 3rd octet only : example from 10.113.XX.31 to

Something like below would help

awk '/^rhts-mgr hostname/ {<replace $3 with g08-rhts[0-9][0-9]>}; print}' file
awk '/^rhts-mgr extern-iface ip-addr/ {<replace $4 10.113.XX.129 with>}; print}' file

I have more than 100 files to edit with each file having this edit to be done. Hence it would be great if we can have faster text processing by doing all replacement scanning one file and replacing it during that scan.

  • It looks like you have 3 IPs that are changed to 53 rather than 55... So, is the 3rd octet really xxx+24 or is that a typo? Commented Oct 24, 2017 at 2:00
  • @RubberStamp its not a typo . the 3 ips are to be changed to .53.The first 5 are .55 in 3rd octet
    – Rocky M
    Commented Oct 24, 2017 at 2:18
  • There are several seemingly contradictory or conflicting points. Your input file has which becomes, for example, but you say later to replace 3rd octet when the pattern is10.113.XX.31. And your sample awk has 'g08-smts' but your sample output has 'g08-rhts'. Suggest you examine closely and clean up the question so it's consistent and clear. Right now some of us find parts of it too vague.
    – B Layer
    Commented Oct 24, 2017 at 2:34
  • @BLayer sorry for the confusing part : it should be rhts, i have edited that. IP in the column needs to have 3rd octet changed to .55 irrespective of numbers .I will remove the last 3 lines as the logic would remain the same . I will use what regex I get as solution to apply to that.
    – Rocky M
    Commented Oct 24, 2017 at 2:46

1 Answer 1



sed -E '
  s/^(rhts-mgr hostname +g)[0-9]+(\-rhts.*)$/\108\2/;
  s/^(rhts-mgr.* 10\.113\.)[0-9]+(\..*)$/\155\2/;
  s/^(rhts-cntrl.* 10\.113\.)[0-9]+(\..*)$/\153\2/
' file


perl -ne '
  s/^(rhts-mgr hostname\s+g)\d+(\-rhts.*)$/${1}08${2}/;
  s/^(rhts-mgr.* 10\.113\.)\d+(\..*)$/${1}55${2}/;
  s/^(rhts-cntrl.* 10\.113\.)\d+(\..*)$/${1}53${2}/;
' file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .