Find the answer to your Linux question:
Results 1 to 7 of 7
hi , I have a text file in which i need to replace all occurances of the word "prod" with "test". I should not replace for the substirng "prod" in ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    2

    sed command to replace all occurances of a word in a file

    hi ,
    I have a text file in which i need to replace all occurances of the word "prod" with "test". I should not replace for the substirng "prod" in a word.
    for example "product" should not be replaced to "testuct"

    I am using the command sed -e 's/prod/test/g' to replace all occurances of the word "prod" with "test".

    but it is replacing "product" to "testuct" which i dont want.

    So how should i change my sed command.


    Thanks in advance

  2. #2
    Just Joined!
    Join Date
    Aug 2009
    Posts
    76
    Quote Originally Posted by ukbhan View Post
    hi ,
    I have a text file in which i need to replace all occurances of the word "prod" with "test". I should not replace for the substirng "prod" in a word.
    for example "product" should not be replaced to "testuct"

    I am using the command sed -e 's/prod/test/g' to replace all occurances of the word "prod" with "test".

    but it is replacing "product" to "testuct" which i dont want.

    So how should i change my sed command.


    Thanks in advance
    Is there a common character that comes after 'prod', such as a space, line break, or quotation mark? If so, then that's your answer right there.

    You could also run another sed command to replace testuct with product.

  3. #3
    Just Joined!
    Join Date
    Jan 2007
    Location
    Tamil Nadu, India
    Posts
    18
    Hi ukbhan,

    sed "s/ prod / test /g" will solve your problem.

  4. #4
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    try (sed -e "s/\<prod\>/test/g") eg:

    echo "prod product cattleprod prod." | sed -e "s/\<prod\>/test/g
    In a world without walls and fences, who needs Windows and Gates?

  5. #5
    Just Joined!
    Join Date
    Jan 2007
    Location
    Tamil Nadu, India
    Posts
    18
    Quote Originally Posted by blinky View Post
    try (sed -e "s/\<prod\>/test/g")

    eg:

    echo "prod product cattleprod prod." | sed -e "s/\<prod\>/test/g
    What does "<" and ">" brackets in "\<prod\>" actually mean here?
    I am sure "\" means you are escaping the brackets.

  6. #6
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    \< = starts with and \> = ends with , used together whole word
    In a world without walls and fences, who needs Windows and Gates?

  7. #7
    Just Joined!
    Join Date
    Jan 2007
    Location
    Tamil Nadu, India
    Posts
    18
    Thanks blinky.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...