הסקריפט הבא בשפת פרל (Perl) מקבל מערך של מחרוזות a ומחליף אותם במערך אחר של מחרוזות b.
לדוגמא את המחרוזת : a0a0a0a0a0a0a0a0a0a0
יחליף הסקריפט במחרוזת: b0b0b0b0b0b0b0b0b0b0
# Invoke the script with by ./script.pl input_file
# You will get a file named input_file, containing your changes, and a file named input_file.bak,
# which is simply a copy of the original file.
#!/usr/bin/perl
use strict;
use warnings;
$^I = '.bak'; # create a backup copy
my @a; # Array of old strings to replace
my @b; # Array of new strings to replace
my $n; # size of array a (and b)
my $j; # index from 0 to n-1
#
$a[0] = 'a0a0a0a0a0a0a0a0a0a0'; # The old string to replace
$b[0] = 'b0b0b0b0b0b0b0b0b0b0'; # The new string
$a[1] = 'a1a1a1a1a1a1a1a1a1a1'; # The old string to replace
$b[1] = 'b1b1b1b1b1b1b1b1b1b1'; # The new string
$n = @a;
while (<>) {
for ($j=0; $j < $n ; $j++) # 'for loop' to replace all strings in each line f the file
{
s/$a[$j]/$b[$j]/g; # do the replacement
}
print; # print to the modified file
}
# You will get a file named input_file, containing your changes, and a file named input_file.bak,
# which is simply a copy of the original file.
#!/usr/bin/perl
use strict;
use warnings;
$^I = '.bak'; # create a backup copy
my @a; # Array of old strings to replace
my @b; # Array of new strings to replace
my $n; # size of array a (and b)
my $j; # index from 0 to n-1
#
$a[0] = 'a0a0a0a0a0a0a0a0a0a0'; # The old string to replace
$b[0] = 'b0b0b0b0b0b0b0b0b0b0'; # The new string
$a[1] = 'a1a1a1a1a1a1a1a1a1a1'; # The old string to replace
$b[1] = 'b1b1b1b1b1b1b1b1b1b1'; # The new string
$n = @a;
while (<>) {
for ($j=0; $j < $n ; $j++) # 'for loop' to replace all strings in each line f the file
{
s/$a[$j]/$b[$j]/g; # do the replacement
}
print; # print to the modified file
}
אין תגובות:
הוסף רשומת תגובה