Guys,
Here I come with the code for the dynamic file update.
Please let me know your comments and concerns with this code.
Thanks for your support and comments.
#################################
#! c:/perl/bin/perl
use File::Copy;
my $file = shift;
my $old = $file;
my $new = "$file.tmp.$$";
my $bak = "$file.bak";
copy($old, $new) or die "can't copy the file:$!";
open(OLD, "< $old") or die "can't open $old: $!";
open(NEW, ">> $new") or die "can't open $new: $!";
print "new file ",$new,"\n\n";
$count=1000;
# Correct typos, preserving case
for($i=1;$i<=$count;$i++) {
seek(OLD,0,0);
while ( <OLD> ) {
#Condition to match the Field ID and incremanet by 20.
#<Field ID="23120">
if ( s/(<Field.*")(\d+)">/ $1 . ( $2 + ($i*20) ) . '">' /e )
{
print "OLD:$2 \t NEW:", $2 + ($i*20), "\n";
}
#Condition to match the line and increament by 1.
# <string ID="Name" Value="24G_L24_UnitsQualifier_2123" />
if ( s/_L(\d{1,3})_/ '_L' . ( $1 + ($i*1) ) . '_' /e )
{
print "OLD:L$1 \t NEW:L", $1 + ($i*1), "\n";
}
#Condition to increment the final segment in the above string
if ( s/(<string.*L\d+.*_)(\d+)"/ $1 . ( $2 + ($i*20) ) . '"' /e )
{
print "OLD:$2 \t NEW:", $2 + ($i*20), "\n";
}
print NEW $_ or die "can't write to $new: $!";
}
print NEW "\n";
}
close(OLD) or die "can't close $old: $!";
close(NEW) or die "can't close $new: $!";
rename($old, $bak) or die "can't rename $old to $bak: $!";
rename($new, $old) or die "can't rename $new to $old: $!";
#################################################
THANKS.
On 2/12/10, Robert Wohlfarth <[email protected]> wrote:
>
> On Fri, Feb 12, 2010 at 3:32 AM, elavazhagan perl <
> [email protected]> wrote:
>
>> *ORIGINAL CONTENT:*
>> <Field ID="*20000*">
>> <AttrSet ID="Core">
>> <integer ID="ZoneId" Value="999" />
>> <string ID="Name" Value="42_*L1*_Valid_Line_Indicator_*20000*" />
>> <boolean ID="LinkedCoord" Value="false" />
>> <integer ID="Type" Value="3" />
>> <integer ID="x" Value="0" />
>> <integer ID="y" Value="0" />
>> <integer ID="w" Value="10" />
>> <integer ID="h" Value="10" />
>> </AttrSet>
>> </Field>
>>
>> *DESIRED OUTPUT:*
>>
>> <Field ID="*20980*">
>> <AttrSet ID="Core">
>> <integer ID="ZoneId" Value="999" />
>> <string ID="Name" Value="42_*L1000*_Valid_Line_Indicator_*20980*"
>> />
>> <boolean ID="LinkedCoord" Value="false" />
>> <integer ID="Type" Value="3" />
>> <integer ID="x" Value="0" />
>> <integer ID="y" Value="0" />
>> <integer ID="w" Value="10" />
>> <integer ID="h" Value="10" />
>> </AttrSet>
>> </Field>
>>
>
> In summary, you want to...
>
> 1. Read an entire file into one string.
> 2. Using a regular expression, get the current value of "Field ID".
> 3. Loop from 2 to 1000...
> 1. Increment "Field ID" by 20.
> 2. Substitute the new "Field ID" for the old one, using regular
> expressions.
> 3. Substitute the counter (Lxxxx) in the <string ID="Name"> field.
> 4. Write the entire block out to a new file.
>
> Is that accurate?
>
> --
> Robert Wohlfarth
>
>