forked from CMSAachen3B/AnalysisTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubs
executable file
·71 lines (59 loc) · 1.6 KB
/
subs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! /usr/bin/perl
#
#############################################################
#
# subs
#
# substitute a string by another in all files in a directory
#
# This script could be replaced by
# sed -i -- 's/replaceThis/byThis/g' fileName*Pattern.*
#
############################################################
#
if (@ARGV < 2) {
print<<HEAD
subs - {inpstr} {outstr} {filenames}
Substitute a string by another in all files in a directory
Syntax :
inpstr - String to substitute
outstr - String to be substituted with
filenames - Filenames (* should expand to all filenames)
Example : To resubs /l3/anal in all files by /shift/shift3
subs \\/l3\\/anal \\/shift\\/shift3 *.job
HEAD
;
}
else {
#
# ### Prepare the input list
#
$inpstr = shift @ARGV ;
$outstr = shift @ARGV ;
@files = @ARGV;
$nfile = @files;
$nmod = 0 ;
foreach $i (@files) {
system(sprintf("grep -s \"%s\" %s ",$inpstr,$i)) ;
if ( $? == 0 ) {
++ $nmod ;
rename("$i","temp.job") || die "can't rename $i to temp.job" ;
open(INPUT,"temp.job") || die "can't open temp.job" ;
open(OUTPUT,">$i") || die "can't open $i" ;
while (<INPUT>) {
$line = $_ ;
$line =~ s/$inpstr/$outstr/g ;
print OUTPUT $line ;
}
close(OUTPUT) ;
close(INPUT) ;
@_ = stat("temp.job") ;
chmod $_[2], "$i" ;
# print "atime $_[8] mtime $_[9] ctime $_[10] $i \n" ;
# utime $_[8], $_[9], "$i" ;
}
# else { print "$? \n" } ;
}
system("rm -f temp.job") ;
print "No of files substituted is $nmod out of $nfile\n" ;
}