Solved

UNIX script to merge xml

Posted on 2013-06-07
19
817 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
  • 8
  • 7
  • 2
  • +2
19 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
it's set to 777, and I own the files...
anything else?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
is output.xml there after just
> output.xml

Is there any result from just
awk '/<\/document>/{system("cat image.xml")}{print}' doc.xml
0
 
LVL 9

Author Comment

by:Evan Cutler
Comment Utility
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
Comment Utility
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
Comment Utility
no...reverse.
I'm saying that cat image.xml produces nothing.

I only see doc.xml
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
it's not empty...
if I do a cat image.xml, I see text.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 9

Author Comment

by:Evan Cutler
Comment Utility
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 167 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 11

Assisted Solution

by:tel2
tel2 earned 166 total points
Comment Utility
> 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 26

Expert Comment

by:skullnobrains
Comment Utility
to insert before, try something like this

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

Expert Comment

by:tel2
Comment Utility
Nice.  I didn't know that.
0
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 167 total points
Comment Utility
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
Comment Utility
Thanks guys...
this is awesome.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now