[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

UNIX script to merge xml

Posted on 2013-06-07
19
Medium Priority
?
1,111 Views
Last Modified: 2013-06-13
awk '/<\/document>/{system("cat image.xml")}{print}' doc.xml > output.xml

Greetings, the above script was recommended by another awesome EE Warrior.  However, for some reason it's not working.  I am using SOLARIS KSH.

I have two xml documents:
<document>
     <header></header>
     <tag1>
          <tag1a></tag1a>
    </tag1>
</document>


<images>
     <image>
          <name></name>
          <size></size>
     </image>
     ....(more images)
</images>


I need to get <images> into <document> like this:
<document>
     <header></header>
     <tag1>
          <tag1a></tag1a>
    </tag1>
     <images>
          <image>
               <name></name>
               <size></size>
          </image>
          ....(more images)
     </images>
</document>


The problem is I get no return. Nada.  This means I don't know what I'm doing wrong.  When I perform a MORE output.xml, it's not there.  
Help?
Thanks.
0
Comment
Question by:Evan Cutler
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 2
  • +2
19 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39229678
If it's not there, rather than there but empty, that indicates that  you had no permission to create the file, or you're looking in the wrong place for the output or .the command did not run at all.
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 39229784
it's set to 777, and I own the files...
anything else?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39229821
is output.xml there after just
> output.xml

Is there any result from just
awk '/<\/document>/{system("cat image.xml")}{print}' doc.xml
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Author Comment

by:Evan Cutler
ID: 39230072
yes...just the original xml..
image.xml is there, but the two don't merge...
any ideas?
Thanks.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39230092
Are you saying that the result of
awk '/<\/document>/{system("cat image.xml")}{print}' doc.xml
is the same as the result of
cat image.xml
?
So the {print} has no effect and doing
awk '/<\/document>/{system("cat image.xml")}{print}' doc.xml > output.xml
is the same as doing
awk '/<\/document>/{system("cat image.xml")}' doc.xml > output.xml
?
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 39230110
no...reverse.
I'm saying that cat image.xml produces nothing.

I only see doc.xml
0
 
LVL 84

Expert Comment

by:ozo
ID: 39230208
if image.xml is empty, then the result of the merging nothing with doc.xml should be the same as  doc.xml
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 39230213
it's not empty...
if I do a cat image.xml, I see text.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39230232
I'm getting confused.
I thought you just said that cat image.xml produces nothing.

You also said that
output.xml, it's not there
so when you said
it's set to 777
, what is set to 777?
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 39230255
sorry...
for this exercise:
awk '/<\/document>/{system("cat image.xml")}{print}' doc.xml > output.xml

image.xml has contents. and is set to 777
doc.xml has the contents and is also set to 777

when I execute this line, output.xml just has the contents of doc.xml.

better?
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 668 total points
ID: 39230395
Does
awk 'BEGIN{system("cat image.xml")}'
produce any output?

Does
awk '/<\/document>/{print}' doc.xml
produce any output?
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 39230421
ok
awk 'BEGIN{system("cat image.xml")}'
causes a pause in the command prompt...no output.

awk '/<\/document>/{print}' doc.xml
gives me </document>

could this be a solaris thing?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39230540
It could be, I don't have a solaris sytem handy to test.
Does the man page for awk on your system say anything about the system function?
Can you use other programs than awk for this purpose?
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 39230544
ok...found the solution...
using SED instead.

sed -e '/<document>/r image.xml' doc.xml>output.xml

it puts it on the top of the XML under <document> instead of the bottom,
but I can work with that.
0
 
LVL 12

Assisted Solution

by:tel2
tel2 earned 664 total points
ID: 39231154
> it puts it on the top of the XML under <document> instead of the bottom, but I can work with that.

Does this put it in the right place, arcee123?
    sed '/<\/tag1>/r image.xml' doc.xml >output.xml
Still not correctly indented, but that's easily fixed (if you want it fixed).
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 39231245
to insert before, try something like this

n
/<\/document>/ r file
p
0
 
LVL 12

Expert Comment

by:tel2
ID: 39231279
Nice.  I didn't know that.
0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 668 total points
ID: 39232022
A slight modification to your original solution works for me:

awk '/\/document/{system("cat image.xml")}{print}' doc.xml > output.xml
0
 
LVL 9

Author Closing Comment

by:Evan Cutler
ID: 39246512
Thanks guys...
this is awesome.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question