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 7943375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:15:23+00:00 2026-06-04T00:15:23+00:00

I am using sed to find and replace items, e.g.: sed -i ‘s/fish/bear/g’ ./file.txt

  • 0

I am using sed to find and replace items, e.g.:

sed -i 's/fish/bear/g' ./file.txt

I want to limit this to only change items which do not have a letter or number before or after, e.g.:

  • The fish ate the worm. would change, because only spaces are before and after.
  • The lionfish ate the worm. would not change, because there is a letter before fish.

How can I find and replace some items, but not if at least one letter or number appears immediately before or after?

  • 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-06-04T00:15:25+00:00Added an answer on June 4, 2026 at 12:15 am

    Use a negative character class before and after fish, like so: \(^\|[^[:alnum:]]\)fish\($\|[^[:alnum:]]\). This says:

    1. Start of line or anything that’s not alphanumeric
    2. Followed by fish
    3. Followed by end of line or anything that’s not alphanumeric

    This guarantees that the characters immediately preceding and immediately following fish are not alphanumeric.

    sed 's/\(^\|[^[:alnum:]]\)fish\($\|[^[:alnum:]]\)/\1bear\2/g'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using sed in a script to do a replace and I want
I am using sed to search and replace two strings in a file in
I'm trying to find replace the following <a href=\test\> with <a href=test> using sed.
Following this questionn Recursively rename files using find and sed I found by myself
I am using sed to replace a line with NULL in a file. The
Using sed, if there is a space character on a line in a file,
UPDATED: Using sed, how can I insert (NOT SUBSTITUTE) a new line on only
I am using sed to concatenate the contents of a file as: cat source.c
I am thinking of using sed for reading .properties file, but was wondering if
I have trouble using sed. I need to replace some lines in very deprecated

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.