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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:51:23+00:00 2026-05-17T20:51:23+00:00

I want to replace the text PATHTOEXPORT with the contents of a variable passed

  • 0

I want to replace the text PATHTOEXPORT with the contents of a variable passed on the command-line that would have a folder path in it (for example, /Dev_Content/AIX/Apache)

I came across this article that discusses how to use sed to replace a value in an XML file with another.

However, not having used sed before, I’m unsure how to read the directions.

The script is as follows:

# Check that exactly 3 values were passed in
if [ $# -ne 3 ]; then
echo 1>&2 “This script replaces xml element’s value with the one provided as a command parameter \n\n\tUsage: $0 <xml filename> <element name> <new value>”
exit 127
fi

echo "DEBUG: Starting... [Ok]\n"
echo "DEBUG: searching $1 for tagname <$2> and replacing its value with '$3'"

# Creating a temporary file for sed to write the changes to
temp_file="repl.temp"

# Elegance is the key -> adding an empty last line for Mr. “sed” to pick up
echo ” ” >> $1

# Extracting the value from the <$2> element
el_value=`grep “<$2>.*<.$2>” $1 | sed -e “s/^.*<$2/<$2/” | cut -f2 -d”>”| cut -f1 -d”<”`

echo "DEBUG: Found the current value for the element <$2> - '$el_value'"

# Replacing elemen’s value with $3
sed -e “s/<$2>$el_value<\/$2>/<$2>$3<\/$2>/g” $1 > $temp_file

# Writing our changes back to the original file ($1)
chmod 666 $1
mv $temp_file $1

Is there a better way to do what I need to do? Can I do it in situ rather than using an intermediate file?

  • 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-17T20:51:24+00:00Added an answer on May 17, 2026 at 8:51 pm

    You can have sed edit in place. There are two options, have sed make a temporary file for you (safest) or really have it edit in place (dangerous if your command isn’t tested).

    sed -i 'bak' -e 's|PATHTOEXPORT|/Dev_Content/AIX/Apache|' file.txt
    

    or for the brave:

    sed -i '' -e 's|PATHTOEXPORT|/Dev_Content/AIX/Apache|' file.txt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On MS SQL Server 2005, I have a table that I want to replace.
i want to replace html text that closed by tag start_ticker code.... end_ticker i
In C#, I want to be able to generically replace text, ignoring case in
I have a template in word (.docx) format and want to replace some placeholders
I have two branches : X and Y. I want to replace a Y's
I want to replace text inside cdata section but when I simply trying to
Possible Duplicate: Eclipse Replace text in all Classes? I want to replace all instances
I am familiar with CSS techniques to replace text with an image. For example,
I want to replace the text in a string Sin() with the computed value
i want to replace the text of a html anchor: <a href=index.html id=link1>Click to

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.