Code Comments
Programming Forum and web based access to our favorite programming groups.webwesen wrote:
> hello awk gurus,
>
> was wondering if awk would be the right tool to attack this prob:
> have file1.xml and file2.xml where
>
> file1.xml contains:
> ...
> <tag attr1="121" attr2="222" attr3="333" />
> <tag attr1="124" attr2="234" attr3="345" />
> <tag attr1="126" attr2="645" attr3="456" />
> <tag attr1="128" attr2="635" attr3="465" />
> ...
>
> file2.xml contains:
> ...
> <tag attr1="121" attr2="342" attr3="546" />
> <tag attr1="126" attr2="789" attr3="465" />
> ...
>
> need to replace matching (by "attr1") lines in file1 with the ones in
> file2
It is possible, but somewhat lengthy...
awk '
BEGIN { attr1pattern = "attr1=\"[0-9]+\""
while (getline <"file2.xml" >0) {
match ($0, attr1pattern)
a[substr($0, RSTART, RLENGTH)] = $0
}
}
{ if (match ($0, attr1pattern)) {
pat = substr($0, RSTART, RLENGTH)
if (pat in a)
print a[pat]
else print
}
else print
}
' <file1.xml
> or should I look into perl::XML::Simple and such? seems like overkill
> to me.
> if someone has some pointers - this also would be very nice.
> thx.
Janis
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.