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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:17:58+00:00 2026-06-02T14:17:58+00:00

I am having trouble figuring out how to use sed to search and replace

  • 0

I am having trouble figuring out how to use sed to search and replace strings containing the / character in a text file /etc/myconfig.

For instance, in my existing text file, I have:

myparam /path/to/a argB=/path/to/B xo

and I want this replaced by:

myparam /path/to/c argB=/path/to/D xo

I attempted doing this in bash:

line='myparam /path/to/a argB=/path/to/B xo'
line_new='myparam /path/to/c argB=/path/to/D xo'
sed -i 's/$line/$line_new/g' /etc/myconfig

But nothing happens.

Attempting

grep -rn "$line" /etc/myconfig

does return me 'myparam /path/to/a argB=/path/to/B xo' though.

What’s the correct way to express my sed command to execute this search and replace and correctly deal with the / command? (I reckon that the / character in my strings are the ones giving me the problem because I used a similar sed command to search and replace another line in the text file with no problems and that line does not have a / character.

  • 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-02T14:18:01+00:00Added an answer on June 2, 2026 at 2:18 pm

    Don’t escape the backslashes; you’ll confuse yourself. Use a different symbol after the s command that doesn’t appear in the text (I’m using % in the example below):

    line_old='myparam /path/to/a argB=/path/to/B xo'
    line_new='myparam /path/to/c argB=/path/to/D xo'
    sed -i "s%$line_old%$line_new%g" /etc/myconfig
    

    Also, enclose the whole string in double quotes; using single quotes means that sed sees $line (in the original) instead of the expanded value. Inside single quotes, there is no expansion and there are no metacharacters. If your text can contain almost any plain text character, use a control character (e.g. control-A or control-G) as the delimiter.

    Note that the use of -i here mirrors what is in the question, but that assumes the use of GNU sed. BSD sed (found on Mac OS X too) requires a suffix. You can use sed -i '' … to replace in situ; that does not work with GNU sed. To be portable between the two, use -i.bak; that will work with both — but gives you a backup file that you’ll probably want to delete. Other Unix platforms (e.g. AIX, HP-UX, Solaris) may have variants of sed that do not support -i at all. It is not required by the POSIX specification for sed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a bit of trouble figuring out the exact syntax to use
I'm having trouble figuring out how to use soundfonts on android (that have a
I'm having some trouble figuring out how to deserialize a binary file. I mostly
I'm having trouble figuring out how could I use my imagebutton (see below) in
I'm having some trouble figuring out how to use more than one left outer
I am having trouble figuring out how to use a sting as an object
I am having trouble figuring out how to use .Include() to be able to
im having trouble figuring out how to bind mouseout() to my entire nav bar
I'm having trouble figuring out how to delete a set of records when a
I'm having trouble figuring out a way to conditionally cancel an asp:TextBox's OnTextChanged AutoPostBack

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.