Exact Text Replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exact Text Replace
# 1  
Old 10-11-2011
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.

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

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


Code:
# '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%
        }

# 2  
Old 10-11-2011
Try:

Code:
sed -i "s/\b$CURRENT\b/$NEW/" "$HOSTFILE"

This User Gave Thanks to radoulov For This Post:
# 3  
Old 10-11-2011
That worked.
Thanks.

Guess I had something wrong in my syntax when I used \b.
# 4  
Old 10-11-2011
Yes, you left it to the shell and it disappeared Smilie

Note the difference:

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

vs.:

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

# 5  
Old 10-11-2011
Oh I see where I messed up.
Thanks again for explaining it, I won't make that mistake again. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - Exact pattern matching and replace

Hi Team, I am facing a problem as under, Suppose I have a file (test.txt) with the below content (all braces and slashes are included in the contents of the file) Now I want to append few words below matched line, I have written the below sed: sed '/option/a insert text here' test... (2 Replies)
Discussion started by: ankur328
2 Replies

2. UNIX for Beginners Questions & Answers

Replace exact word by perl

my inputfile: This is a test and only a test for production - prod. echo "This is a test and only a test for production - prod" | perl -pi -e 's/prod/test/g' outputfile: This is a test and only a test for testuction - test I only want to replace prod, not production. I also... (3 Replies)
Discussion started by: loktamann
3 Replies

3. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

4. Shell Programming and Scripting

How to use SED or AWK to search and replace an exact string

I have a file DS1 DDS DS I want to replace only "DS" to "DSmail.blah.com" in a lot of files. I tried sed 's/DS/DSmail.blah.com' but it changes all the lines . thanks in advance (2 Replies)
Discussion started by: gubbu
2 Replies

5. Shell Programming and Scripting

search and replace exact string

Hello Everyone, Im trying to run a search and replace of exact strings and the strings that im using variables that are passed through an array in a while loop. Here is a snip of my code: USEROLD=`cat oldusers` USERNEW=`cat newusers` USEROLDARRAY=( $USEROLD ) USERNEWARRAY=( $USERNEW )... (4 Replies)
Discussion started by: skizim
4 Replies

6. Shell Programming and Scripting

Exact Search and Replace using AWK

Hello. I have written the following script to search and replace from one file into another. #awk script to search and replace from file a in file b NR == FNR { A=$2; next } { for( a in A ) sub(a, A)}1 file2 file1 While the function works pretty well, I want a. The word in File 2 to... (8 Replies)
Discussion started by: gimley
8 Replies

7. Shell Programming and Scripting

SED to replace exact match, not first occurrence.

Lets say I have file.txt: (Product:Price:QuantityAvailable) (: as delimiter) Chocolate:5:5 Banana:33:3 I am doing a edit/update function. I want to change the Quantity Available, so I tried using the SED command to replace 5, but my Price which is also 5 is changed instead. (for the Banana... (13 Replies)
Discussion started by: andylbh
13 Replies

8. Shell Programming and Scripting

Urgent help needed !!!....to replace a exact string

Hi experts, As i am a novice unix player...so need help for the below query...banged my head from quite a while...:confused: i have a set of html files, in which i need to search for string "Page"(case sensitive) and then replace the same with some numeric code ,say, "XXX1234". Here in... (1 Reply)
Discussion started by: rahulfhp
1 Replies

9. UNIX for Dummies Questions & Answers

Replace based on an exact position

Trying to use sed - but having no luck. I have a text file - I want to replace whatever character is in position 106, 157 and 237 w/ the string "xxx". Want this change for all lines w/in that text file. I'm open to using awk or whatever command would be best for replacing characters based... (5 Replies)
Discussion started by: svn
5 Replies

10. Shell Programming and Scripting

Replace exact word with blank

i have a file with the below content file1.txt ALERTADMIN.FIELD ALERTADMIN.TX_ALERTS_LOG i have another file file2 ALERTADMIN.FIELD ALERTADMIN.FIELD_WS ALERTADMIN.SECTION_FIELD_WS ALERTADMIN.TX_ACCT_PROCESSING_WORK_TABLE ALERTADMIN.TX_ACCT_REVIEW_EXEC_METRICS... (2 Replies)
Discussion started by: lavnayas
2 Replies
Login or Register to Ask a Question