2015-10-10 04:55:57 +03:00
|
|
|
#!/usr/bin/tcl
|
|
|
|
#
|
|
|
|
# Replace string with another string -OR- include
|
|
|
|
# only lines successfully modified with a regular
|
|
|
|
# expression.
|
|
|
|
#
|
2016-08-02 22:29:56 +03:00
|
|
|
fconfigure stdout -translation binary -encoding binary
|
|
|
|
fconfigure stderr -translation binary -encoding binary
|
2015-10-10 04:55:57 +03:00
|
|
|
set mode [string tolower [lindex $argv 0]]
|
|
|
|
set from [lindex $argv 1]
|
|
|
|
set to [lindex $argv 2]
|
2021-09-11 01:00:43 +03:00
|
|
|
if {-1 == [lsearch -exact [list exact regsub include] $mode]} {exit 1}
|
2015-10-10 04:55:57 +03:00
|
|
|
if {[string length $from]==0} {exit 2}
|
|
|
|
while {![eof stdin]} {
|
|
|
|
set line [gets stdin]
|
|
|
|
if {[eof stdin]} break
|
|
|
|
switch -exact $mode {
|
|
|
|
exact {set line [string map [list $from $to] $line]}
|
2016-07-28 17:37:04 +03:00
|
|
|
regsub {regsub -all -- $from $line $to line}
|
2015-10-10 04:55:57 +03:00
|
|
|
include {if {[regsub -all -- $from $line $to line]==0} continue}
|
|
|
|
}
|
|
|
|
puts stdout $line
|
|
|
|
}
|