Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7029487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:32:44+00:00 2026-05-28T00:32:44+00:00

I need to replace several words with other words. For e.g.: apple with FRUIT

  • 0

I need to replace several words with other words.

For e.g.: “apple” with “FRUIT” in file, only in these 4 situations:

  • _apple_, has a blank space before and after.
  • [apple_, has a square opening bracket before and a blank space after.
  • _apple], has a blank space before and a square closing bracket after.
  • [apple], has square brackets before and after.

I do not want the replaces to occur in any other situation.

I have tried using the following code:

a="apple"
b="fruit"
sed -i "s/ $a / $b /g" ./file
sed -i "s/\[$a /\[$b /g" ./file
sed -i "s/ $a\]/ $b\]/g" ./file
sed -i "s/\[$a\]/\[$b\]/g" ./file

I thought the option “g” at the end would mean it would replace all instances, but I found this is not a thorough solution. For e.g. if file contains this:

apple spider apple apple spider tree apple tree

The third occurrence of “apple” is not being replaced. Also in this, several appearances of the word are not changed:

apple  spider apple apple apple apple apple spider tree apple tree

I suspect this is because the shared “space”.

How can I get this to find and replace all instances of $a with $b, regardless of any overlap?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:32:44+00:00Added an answer on May 28, 2026 at 12:32 am

    The quick-and-dirty solution is to perform the replacement twice.

    $ echo apple apple apple apple[apple apple] | sed -e 's/\(\[\| \)apple\( \|\]\)/\1FRUIT\2/g; s/\(\[\| \)apple\( \|\]\)/\1FRUIT\2/g'
    apple FRUIT FRUIT apple[FRUIT FRUIT]
    

    This is safe because, after the first command, the resulting text won’t contain any occurrences of (\[| )apple( |\]) that were not already in the original text.

    The drawback is that two replacements take roughly twice more time to run.

    If you break it in two executions of sed, you can see the steps clearer:

    $ echo apple apple apple apple apple apple[apple apple] | sed -e 's/\(\[\| \)apple\( \|\]\)/\1FRUIT\2/g'
    apple FRUIT apple FRUIT apple apple[FRUIT apple]
    
    $ echo apple FRUIT apple FRUIT apple apple[FRUIT apple] | sed -e 's/\(\[\| \)apple\( \|\]\)/\1FRUIT\2/g'
    apple FRUIT FRUIT FRUIT FRUIT apple[FRUIT FRUIT]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to replace several URLs in a text file with some content dependent
I'd like to replace the format of words matching words in another txt-file. I
I have an array that contains several domain names. I need to replace those
I'm trying to perform a search-replace in several JCLs but I need multi-line capabilities,
I have several files, and I need to replace third line in them: files
I just need to replace these 18 functions with one functions that does the
I have several variables like these: $foo = '123'; $bar = 'bqwe'; I need
I need replace slide effect to fade-out/fade-in effect on the prev and next slide
Need to replace a domain name on all the links on the page that
We need to replace the menu system in our main ASP.NET application. So naturally

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.