mistachkin | 946ef60 | 2015-10-10 01:55:57 +0000 | [diff] [blame] | 1 | #!/usr/bin/tcl |
| 2 | # |
| 3 | # Replace string with another string -OR- include |
| 4 | # only lines successfully modified with a regular |
| 5 | # expression. |
| 6 | # |
mistachkin | 3dffcf9 | 2016-08-02 19:29:56 +0000 | [diff] [blame] | 7 | fconfigure stdout -translation binary -encoding binary |
| 8 | fconfigure stderr -translation binary -encoding binary |
mistachkin | 946ef60 | 2015-10-10 01:55:57 +0000 | [diff] [blame] | 9 | set mode [string tolower [lindex $argv 0]] |
| 10 | set from [lindex $argv 1] |
| 11 | set to [lindex $argv 2] |
mistachkin | 52b1dbb | 2016-07-28 14:37:04 +0000 | [diff] [blame] | 12 | if {$mode ni [list exact regsub include]} {exit 1} |
mistachkin | 946ef60 | 2015-10-10 01:55:57 +0000 | [diff] [blame] | 13 | if {[string length $from]==0} {exit 2} |
| 14 | while {![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]} |
mistachkin | 52b1dbb | 2016-07-28 14:37:04 +0000 | [diff] [blame] | 19 | regsub {regsub -all -- $from $line $to line} |
mistachkin | 946ef60 | 2015-10-10 01:55:57 +0000 | [diff] [blame] | 20 | include {if {[regsub -all -- $from $line $to line]==0} continue} |
| 21 | } |
| 22 | puts stdout $line |
| 23 | } |