Exact Text Replace

I am currently working with a bash script to change some names around in 3 files. I am attempting to do this with sed but I haven't been able to get it so it won't replace partial matches.

Below is an example of the files I am trying to edit.

My main goal is to replace foo with test, but I am getting it where either it won't replace it at all or I also end up with test_1 because it edits foo_1.

I have tried the following codes, and a few more that totally didn't work.

Thanks for any help on this.

/bin/sed -i '/'${CURRENT}'/ s|'\<${CURRENT}\>'|'${NEW}'|' $HOSTFILE

/bin/sed -i '/'${CURRENT}'/ s|'\b${CURRENT}\b'|'${NEW}'|' $HOSTFILE


# 'FOO_1' host definition
define host{
        use                     generic-host            ; Name of host template to use
        host_name               FOO_1
        alias                   FOO_1
        address                 192.168.1.1
        check_command           check-host-alive
        _cktID                  Test
        max_check_attempts      3
        check_interval          5
        retry_interval          1
        check_period            24x7
        notification_interval   120
        notification_period     24x7
        notification_options    d,u,r
        contact_groups          TESTGROUP
        }

# 'FOO' host definition
define host{
        use                     generic-host            ; Name of host template to use
        host_name               FOO_2
        alias                   FOO_2
        address                 192.168.1.2
        check_command           check-host-alive
        _cktID                  Test
        max_check_attempts      3
        check_interval          5
        retry_interval          1
        check_period            24x7
        notification_interval   120
        notification_period     24x7
        notification_options    d,u,r
        contact_groups          TESTGROUP
        }

--------------------------------------------------------------------------

define hostgroup{
        hostgroup_name  TestGroup
        alias           TestGroup
        members         Local,FOO_1,FOO
        }

--------------------------------------------------------------------------


# FOO_1 service definition
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       FOO_1
        service_description             PING
        is_volatile                     0
        check_period                    24x7
        max_check_attempts              3
        normal_check_interval           5
        retry_check_interval            1
        contact_groups                  TESTGROUP
        notification_interval           120
        notification_period             Hours
        notification_options            c,r
        check_command                   check_ping!100.0,20%!2000.0,60%
        }

# FOO service definition
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       FOO_2
        service_description             PING
        is_volatile                     0
        check_period                    24x7
        max_check_attempts              3
        normal_check_interval           5
        retry_check_interval            1
        contact_groups                  TESTGROUP
        notification_interval           120
        notification_period             Hours
        notification_options            c,r
        check_command                   check_ping!100.0,20%!2000.0,60%
        }

Try:

sed -i "s/\b$CURRENT\b/$NEW/" "$HOSTFILE"
1 Like

That worked.
Thanks.

Guess I had something wrong in my syntax when I used \b.

Yes, you left it to the shell and it disappeared :slight_smile:

Note the difference:

/bin/sed -i '/'${CURRENT}'/ s|'\b${CURRENT}\b'|'${NEW}'|' $HOSTFILE

vs.:

/bin/sed -i '/'${CURRENT}'/ s|\b'${CURRENT}'\b|'${NEW}'|' $HOSTFILE

Oh I see where I messed up.
Thanks again for explaining it, I won't make that mistake again. :slight_smile: