0

I have a XML file in following format:

 <Country name=England>
  <City name_city=London>
  some thing here
  </City>
  <City name_city=Manchester>
  some thing here
  </City>
  <City name_city=Liverpool>
  some thing here
  </City>
  Many other elements like these ones.
</Country>

And another file like this:

  <City name_city=Hull>
  some thing here
  </City>

Using bash script, how can I insert content of the later file in to the first one at after the element

  <City name_city=Manchester>
  some thing here
  </City>

I'm looking forward to using SED or AWK to solve the issue.

Thanks for your help.

1
  • 4
    It is no good idea to use Sed or Awk to modify XML. Sed and Awk are for line oriented data. XML is no line oriented data. You have to restrict your XML which will sooner or later break your application. You should consider an XSLT based solution. Try Saxon. Commented Mar 28, 2014 at 15:17

1 Answer 1

1

Parsing xml using sed and awk is never recommended. There are better tools out three. If it is a learning exercise then you can do the following, but like I said, use proper xml parsers.

awk '
/City name_city=Manchester>/ { flag = 1 } 
flag && /<\/City>/ { 
    print $0; 
    while((getline line < "file2") > 0) { 
        print line; 
    }
    flag=0
    next
}1' file1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.