From: Thorsten Hofrichter on
I need some help trying to find an easy way to do this. Can use either
sed/awk/perl , dont really care. :
I have a file with some data in it , Lets call it abc.txt
Then I have another file ( XML format ) , and would like to find a
pattern and insert abc.txt in it .

For example :
abc.txt contains :
<abc>This is a test</abc>

original.xml :
<properties>
<abc>This is 1</abc>
<abc>This is 2</abc>
</properties>

I want to insert abc.txt somewhere in the properties. I want the
result to be this.. ( I dont care if it is first, second , or last
entry ) :
<properties>
<abc>This is a test</abc>
<abc>This is 1</abc>
<abc>This is 2</abc>
</properties>


Any help would be great. Thanks
From: Janis Papanagnou on
On 23/06/10 20:09, Thorsten Hofrichter wrote:
> I need some help trying to find an easy way to do this. Can use either
> sed/awk/perl , dont really care. :
> I have a file with some data in it , Lets call it abc.txt
> Then I have another file ( XML format ) , and would like to find a
> pattern and insert abc.txt in it .
>
> For example :
> abc.txt contains :
> <abc>This is a test</abc>
>
> original.xml :
> <properties>
> <abc>This is 1</abc>
> <abc>This is 2</abc>
> </properties>
>
> I want to insert abc.txt somewhere in the properties. I want the
> result to be this.. ( I dont care if it is first, second , or last
> entry ) :
> <properties>
> <abc>This is a test</abc>
> <abc>This is 1</abc>
> <abc>This is 2</abc>
> </properties>
>
>
> Any help would be great. Thanks

Hmm... - I hope there's no hidden requirements; given a fixed and well
formatted data like the above, this sounds not too complicated.

awk '
NR==FNR { x=$0 ; next }
f { print " "x ; f=0 }
/<properties>/ {f=1}
{ print }
' abc.txt original.xml

Or if the file abc.txt contains more than one line, one possibility is

awk '
NR==FNR { n=NR ; x[n]=$0 ; next }
f { for(i=1; i<=n; i++) print " "x[i] ; f=0 }
/<properties>/ {f=1}
{ print }
' abc.txt original.xml


Janis
From: Thorsten Hofrichter on
On Jun 23, 6:27 pm, Janis Papanagnou <janis_papanag...(a)hotmail.com>
wrote:
> On 23/06/10 20:09, Thorsten Hofrichter wrote:
>
>
>
>
>
> > I need some help trying to find an easy way to do this. Can use either
> > sed/awk/perl , dont really care.  :
> > I have a file with some data in it , Lets call it abc.txt
> > Then I have another file ( XML format ) , and would like to find a
> > pattern and insert abc.txt in it .
>
> > For example :
> > abc.txt contains :
> > <abc>This is a test</abc>
>
> > original.xml :
> > <properties>
> >      <abc>This is 1</abc>
> >      <abc>This is 2</abc>
> > </properties>
>
> > I want to insert abc.txt somewhere in the properties. I want the
> > result to be this.. ( I dont care if it is first, second , or last
> > entry ) :
> > <properties>
> >      <abc>This is a test</abc>
> >      <abc>This is 1</abc>
> >      <abc>This is 2</abc>
> > </properties>
>
> > Any help would be great. Thanks
>
> Hmm... - I hope there's no hidden requirements; given a fixed and well
> formatted data like the above, this sounds not too complicated.
>
>   awk '
>      NR==FNR { x=$0 ; next }
>      f { print "     "x ; f=0 }
>      /<properties>/ {f=1}
>      { print }
>   ' abc.txt original.xml
>
> Or if the file abc.txt contains more than one line, one possibility is
>
>   awk '
>      NR==FNR { n=NR ; x[n]=$0 ; next }
>      f { for(i=1; i<=n; i++) print "     "x[i] ; f=0 }
>      /<properties>/ {f=1}
>      { print }
>   ' abc.txt original.xml
>
> Janis- Hide quoted text -
>
> - Show quoted text -

This is so close... The only issue is that I multiple <properties> in
the file. I should have mentioned that . I oversimplified my example.
How do I make it add it only for the first <properties> it finds.
So start file looks like
<properties>
<abc>This is 1</abc>
<abc>This is 2</abc>
</properties>
<properties>
<def>Line 1</def>
<def>Line 2</def>
</properties>
From: Thorsten Hofrichter on
This is so close... The only issue is that I multiple <properties> in
the file. I should have mentioned that . I oversimplified my example.
How do I make it add it only for the first <properties> it finds.
So start file looks like
<properties>
<abc>This is 1</abc>
<abc>This is 2</abc>
</properties>
<properties>
<def>Line 1</def>
<def>Line 2</def>
</properties>
From: Thorsten Hofrichter on
Got a solution to this ... THANKS a ton. I was able to modify it to
add the logic to only do this for the first occurance.
I learned a lot from the NR=FNR line. Never done that before. Way
cool.