From: Hermann Peifer on
On 24/06/2010 14:00, Thorsten Hofrichter wrote:
> 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
>>
>
> 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>


You could simply make a small change in this line:

/<properties>/ && ++cnt == 1 {f=1}

You should also be aware that XML data processing with sed, awk, etc. in
more a hack than a sustainable solution.

Hermann
From: Rakesh Sharma on
On Jun 24, 5:01 pm, Thorsten Hofrichter <thh...(a)gmail.com> wrote:
> 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>


Try this:

sed -e '
/<properties>/!b
r abc.txt
:loop
n
bloop
' yourxmlfile