-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace
More file actions
executable file
·51 lines (42 loc) · 1.24 KB
/
Copy pathreplace
File metadata and controls
executable file
·51 lines (42 loc) · 1.24 KB
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
46
47
48
49
50
51
#!/usr/bin/perl -s
use strict;
use warnings;
# -l automatically chomps the input record, and sets R\ equal to $/.
# I was having problems with ^M (CR) characters showing up randomly
# 10/17/2020 I still have not solved this problem entirely.
# Do some perl operation on a bunch of files, editing those files in place.
if (($ARGV[0] eq "") || ($ARGV[1] eq "")) {
print "Usage: replace perlexpr file [file ...]\n";
exit(1);
}
my $op = shift;
my $i = 0;
foreach $i (0 .. $#ARGV) {
print "Argument ", $i, " = ==>", $ARGV[$i], "<==\n";
}
print "Applying '$op' to\n";
print "@ARGV\n";
foreach $i (0 .. $#ARGV) {
my $filename = $ARGV[$i];
my $filenamebak = $filename . ".bak";
if (-d $filename) {
print "Cannot process directory $filename\n";
next;
}
# rename the file to .bak
if (!rename ($filename, $filenamebak)) {
die "Cannot backup $filename : $!";
}
my $fin, my $fout;
# open the new .bak file for input
if (!open($fin, "<:raw", $filenamebak)) {
die "Cannot open $filenamebak : $!";
}
# and prepare to write modified contents to the original filename
open($fout, ">:raw", $filename);
# do the replace and write line by line
while (<$fin>) {
eval $op;
print $fout $_;
}
}