blob: 1ecc6dc7b5184251ead8fd17b209b43d1e127784 [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#
7set mode [string tolower [lindex $argv 0]]
8set from [lindex $argv 1]
9set to [lindex $argv 2]
mistachkin52b1dbb2016-07-28 14:37:04 +000010if {$mode ni [list exact regsub include]} {exit 1}
mistachkin946ef602015-10-10 01:55:57 +000011if {[string length $from]==0} {exit 2}
12while {![eof stdin]} {
13 set line [gets stdin]
14 if {[eof stdin]} break
15 switch -exact $mode {
16 exact {set line [string map [list $from $to] $line]}
mistachkin52b1dbb2016-07-28 14:37:04 +000017 regsub {regsub -all -- $from $line $to line}
mistachkin946ef602015-10-10 01:55:57 +000018 include {if {[regsub -all -- $from $line $to line]==0} continue}
19 }
20 puts stdout $line
21}