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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:53:07+00:00 2026-06-11T12:53:07+00:00

I am trying to replace some text in a file replacetest.xml here is the

  • 0

I am trying to replace some text in a file replacetest.xml

here is the part of the file I want to modify.

<class name="replace_after_this_string">randomtext</class>

I want to change the text randomtext with the text at the index of my array

orgs=( item1 item2 )

and overwrite the file with these modifications.

My main issues are with wildcards and the sed command. So here is what I tried

orgs=( item1 item2 )

SRC="name="'"replace_after_this_string"'">"
#need some sort of wildcard here


for i in "${orgs[@]}"
do
:
    # do whatever on $i
    DST=$SRC$i

    sed -e 's/$SRC/$DST/g' -i replacetest.xml

done

1) I need a wildcard to designate that I want to replace randomtext after identifying what will be in the variable $SRC

2) My sed statement doesn’t do anything except print to command line verbatim, does not modify my file at all, even in what is printed to command line

  • 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-11T12:53:08+00:00Added an answer on June 11, 2026 at 12:53 pm

    I think sed is the wrong tool for this, since it doesn’t understand the structure of XML files, and as a result, your script will end up being rather brittle even if you can get it to work at all. As Brian Agnew suggests in his answer, xmlstarlet is a helpful tool for manipulating XML files. For example, if your file (12477913.xml, say) is as follows:

    <?xml version="1.0"?>
    <foo>
      <class name="replace_after_this_string">randomtext</class>
      <class name="not_for_replacing">some other text</class>
    </foo>
    

    … then this command:

    xmlstarlet ed -u './/class[@name="replace_after_this_string"]' \
        -v 'REPLACED HERE' 12477913.xml
    

    … would produce the following output:

    <?xml version="1.0"?>
    <foo>
      <class name="replace_after_this_string">REPLACED HERE</class>
      <class name="not_for_replacing">some other text</class>
    </foo>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to replace/modify some of the text nodes in an XML document by
I'm trying to replace some text in a file with a value. Everything works
I've a file with some text (8/9 lines) called abc.txt I'm trying to replace
I want to replace some text in every script file in folder, and I'm
I am trying to replace some text in a file with python 2.6 However,
I am trying to use a macro to find and replace some text in
I'm trying to replace parts of a Val() in jquery with some text but
I'm trying to get file contents, replace some parts of it using regular expressions
I am trying to search and replace some of the text (eg 'Smith, John')
I need to replace several URLs in a text file with some content dependent

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.