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

  • Home
  • SEARCH
  • 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 6806125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:40:20+00:00 2026-05-26T19:40:20+00:00

I want to run a shell script with this usage: ./run A.txt B.xml A.txt

  • 0

I want to run a shell script with this usage:

./run A.txt B.xml

A.txt contain some statistic:

Accesses = 1
Hits = 2
Misses = 3
Evictions = 4
Retries = 5

B.xml looks like:

<stat name="total_accesses" value="0"/>
<stat name="total_misses" value="0"/>
<stat name="conflicts" value="0"/>  

I want to replace some stats in B.xml from A.txt. For example, I want to

1- find "Accesses" in A.txt
2- find "total_accesses" in B.xml
3- replace 0 with 1
1- find "Misses" in A.txt
2- find "total_misses" in B.xml
3- replace 0 with 3

So B.xml will look like:

<stat name="total_accesses" value="1"/>
<stat name="total_misses" value="3"/>
<stat name="conflicts" value="0"/>  

I want to do that with shell “sed” command. However I find it quite complex as the regexp is hard to understand.

Does “sed” help me in this problem or I have to find another way?

  • 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-26T19:40:20+00:00Added an answer on May 26, 2026 at 7:40 pm

    It might be a bit heavy-weight for such a simple case, but here’s a Python script that does the job:

    #!/usr/bin/env python
    import sys
    import xml.etree.ElementTree as etree
    
    # read A.txt; fill stats
    stats = {}
    for line in open(sys.argv[1]):
        if line.strip():
            name, _, count = line.partition('=')
            stats["total_"+name.lower().strip()] = count.strip()
    
    # read B.xml; fix to make it a valid xml; replace stat[@value]
    root = etree.fromstring("<root>%s</root>" % open(sys.argv[2]).read())
    for s in root:
        if s.get('name') in stats:
            s.set('value', stats[s.get('name')])
        print etree.tostring(s),
    

    Example

    $ python fill-xml-template.py A.txt B.xml 
    <stat name="total_accesses" value="1" />
    <stat name="total_misses" value="3" />
    <stat name="conflicts" value="0" /> 
    

    To process input files incrementally or to makes changes inplace you could use the following:

    #!/usr/bin/env python
    import fileinput
    import sys
    import xml.etree.ElementTree as etree
    
    try: sys.argv.remove('-i')
    except ValueError: 
        inplace = False
    else: inplace = True # make changes inplace if `-i` option is specified
    
    # read A.txt; fill stats
    stats = {}
    for line in open(sys.argv.pop(1)):
        if line.strip():
            name, _, count = line.partition('=')
            stats["total_"+name.lower().strip()] = count.strip()
    
    # read input; replace stat[@value]
    for line in fileinput.input(inplace=inplace):
        s = etree.fromstring(line)
        if s.get('name') in stats:
            s.set('value', stats[s.get('name')])
        print etree.tostring(s)
    

    Example

    $ python fill-xml-template.py A.txt B.xml -i
    

    It can read from stdin or process several files:

    $ cat B.xml | python fill-xml-template.py A.txt
    <stat name="total_accesses" value="1" />
    <stat name="total_misses" value="3" />
    <stat name="conflicts" value="0" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to execute following command in shell script cp /somedire/*.(txt|xml|xsd) /destination/dir/ But this
I want to run a shell script in php, but this shell script takes
My $SHELL is tcsh. I want to run a C shell script that will
I am trying to run a jar using this command in my shell script
I'm trying to write a shell script that, when run, will set some environment
I've run into a really silly problem with a Linux shell script. I want
I have a shell script which I want to run without using the sh
I want to run a java program using shell script. The java program is
I want to run a shell command in Terminal, then clear the console, from
I want run a script as follows: runner: ssh 'java program &' ssh 'java

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.