blob: 5a1ac5983c44c9233aa7e954121d7e38f6f30f29 [file] [log] [blame]
mistachkin946ef602015-10-10 01:55:57 +00001#!/usr/bin/tcl
2#
3# Replace string with another string -OR- include
4# only lines successfully modified with a regular
5# expression.
6#
mistachkin3dffcf92016-08-02 19:29:56 +00007fconfigure stdout -translation binary -encoding binary
8fconfigure stderr -translation binary -encoding binary
mistachkin946ef602015-10-10 01:55:57 +00009set mode [string tolower [lindex $argv 0]]
10set from [lindex $argv 1]
11set to [lindex $argv 2]
mistachkin52b1dbb2016-07-28 14:37:04 +000012if {$mode ni [list exact regsub include]} {exit 1}
mistachkin946ef602015-10-10 01:55:57 +000013if {[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]}
mistachkin52b1dbb2016-07-28 14:37:04 +000019 regsub {regsub -all -- $from $line $to line}
mistachkin946ef602015-10-10 01:55:57 +000020 include {if {[regsub -all -- $from $line $to line]==0} continue}
21 }
22 puts stdout $line
23}