-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_carets.tcl
More file actions
71 lines (68 loc) · 1.6 KB
/
remove_carets.tcl
File metadata and controls
71 lines (68 loc) · 1.6 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/tclsh
#
# Usage:
#
# tclsh remove_carets.tcl [-at <pathnames_file>] <glob patterns>
#
# This script scans all specified files and removes certain
# character sequences from them. The files specified are
# those glob-matching the given <glob patterns> and those
# named, one per line, in the <pathnames_file> (if any.)
# Character sequences removed are:
#
# ^(
# )^
# ^
#
if {$argc < 1} {
puts stderr "Removing no ^ characters."
exit 1
}
if {[lindex $argv 0] eq "-v"} {
set sayStats 1
set argv [lreplace $argv 0 0]
incr argc -1
} else {set sayStats 0}
if {[lindex $argv 0] eq "-at"} {
if {$argc < 2} {
puts stderr "Bad remove_carets invoke."
exit 1
}
set fnfd [open [lindex $argv 1] r]
set flist [split [read $fnfd] "\n"]
close $fnfd
set files [lreplace $flist end end]
set fla 2
} else {
set files [list]
set fla 0
}
foreach fgspec [lrange $argv $fla end] {
lappend files {*}[glob $fgspec]
}
set fcount 0
set rcount 0
array set fstats {}
set killPattern {(\^\()|(\)\^)|\^}
if {!$sayStats} {puts -nonewline {Removing ^( )^ and ^ ...}}
foreach fm $files {
if {![info exists fstats($fm)]} {
set ifd [open $fm r]
set text [regsub -all $killPattern [read $ifd] {}]
set shrink [expr [file size $fm] - [string length $text]]
set fstats($fm) $shrink
close $ifd
incr fcount
if {$shrink > 0 && !$sayStats} {
set ofd [open $fm w]
puts -nonewline $ofd $text
close $ofd
incr rcount
}
}
}
if {$sayStats} {
foreach fm [array names fstats] {
puts "$fm,$fstats($fm)"
}
} else {puts "\b\b\bfrom $rcount of $fcount files."}