1#!/usr/bin/tcl 2# 3# Replace string with another string -OR- include 4# only lines successfully modified with a regular 5# expression. 6# 7fconfigure stdout -translation binary -encoding binary 8fconfigure stderr -translation binary -encoding binary 9set mode [string tolower [lindex $argv 0]] 10set from [lindex $argv 1] 11set to [lindex $argv 2] 12if {-1 == [lsearch -exact [list exact regsub include] $mode]} {exit 1} 13if {[string length $from]==0} {exit 2} 14while {![eof stdin]} { 15 set line [gets stdin] 16 if {[eof stdin]} break 17 switch -exact $mode { 18 exact {set line [string map [list $from $to] $line]} 19 regsub {regsub -all -- $from $line $to line} 20 include {if {[regsub -all -- $from $line $to line]==0} continue} 21 } 22 puts stdout $line 23} 24