forked from Avix101/Mail-Server-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perl-find-replace
executable file
·45 lines (35 loc) · 1.03 KB
/
perl-find-replace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/perl
#Open a file, find a string and replace it
#First arg: What to look for (include escape chars)
#Second arg: What to replace the match with (don't include escape chars)
#Third arg; What file to use
#All args in single quotes
#Author: Avix
use strict;
use warnings;
my $find = $ARGV[0];
my $replace = $ARGV[1];
my $filename = $ARGV[2];
print "Finding... $find\n";
print "Replacing with... $replace\n";
print "In File... $filename\n";
my $data = read_file($filename);
$data =~ s/$find/$replace/g;
write_file($filename, $data);
print "File Overwrite Complete!\n";
exit;
sub read_file {
my ($filename) = @_;
open my $in, '<:encoding(UTF-8)', $filename or die "Could not open '$filename' for reading $!";
local $/ = undef;
my $all = <$in>;
close $in;
return $all;
}
sub write_file {
my ($filename, $content) = @_;
open my $out, '>:encoding(UTF-8)', $filename or die "Could not open '$filename' for writing $!";;
print $out $content;
close $out;
return;
}