1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ServiceId.suffix" value="_TEST" />
</appSettings>
</configuration>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
my $searchStr2 = "value=\".*\""; my $suffix2 = "scheisse"; my $replaceStr2 = "value=\"_" . $suffix2 . "\""; replaceInFile("f:\\tmp\\myfile.config",$searchStr2,$replaceStr2); sub replaceInFile{ my($file,$searchStr,$replaceStr) = @_; my @content = &getFileContent($file); foreach my $line(@content){ print "line --> $line"; $line = s/$searchStr/$replaceStr/g; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
use strict; use warnings; use File::Slurp; my $searchStr = "value=\".*\""; my $suffix = "YEPPA"; my $replaceStr = "value=\"_" . $suffix . "\""; my @lines = read_file( \*DATA ); foreach my $line(@lines){ print "line --> $line"; $line =~ s/$searchStr/$replaceStr/g; print "line --> $line"; } __DATA__ <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="ServiceId.suffix" value="_TEST" /> </appSettings> </configuration>
1
2
3
4
5
6
7
8
9
10
11
12
13
E:\develop>perl test.pl
line --> <?xml version="1.0" encoding="utf-8" ?>
line --> <?xml version="1.0" encoding="utf-8" ?>
line --> <configuration>
line --> <configuration>
line --> <appSettings>
line --> <appSettings>
line --> <add key="ServiceId.suffix" value="_TEST" />
line --> <add key="ServiceId.suffix" value="_YEPPA" />
line --> </appSettings>
line --> </appSettings>
line --> </configuration>
line --> </configuration>
1 2 3 4 5 6 7 8 9 10 11 12 13
sub getFileContent{ my ($input,$enc) = @_; my $defaultEncoding = "utf8"; my $encoding = ($enc) ? "encoding($enc)" : "encoding($defaultEncoding)"; my @content; open(DAT, $input) || die("Could not open file $input $!"); binmode(DAT, "$encoding"); @content=<DAT>; close(DAT); return @content; }