With Tcl, you'd write a somewhat longer program than in those other languages:
# Open the files for streaming...
set filename "whatever.log"
set fin [open $filename]
set fout [file tempfile tempName]
# Process the lines, one by one...
while {[gets $fin line] >= 0} { ### <<< THIS IS IDIOMATIC FOR STREAMING
if {[string first "-s 0 -d 29 -p cbr" $line]} {
regsub "^h" $line "d" line
}
puts $fout $line
}
# Finalize everything
close $fin
close $fout
file rename $tempName $filename
If you want the output to go to a different file, you can just use:
set fout [open "differentFile.txt" "w"]
instead of the set fout [file tempfile tempName]
and omit the file rename
. (That will also make the code work on all versions of Tcl earlier than 8.6; file tempfile
is a new feature, but everything else used here has been around for ages.)
Alternatively, for a version that reads in all the lines at once, replace the central processing loop with this one-liner that uses line-mode RE substitution and a little bit of smartness:
# Use [puts -nonewline] because the last newline is kept with [read]
puts -nonewline $fout [regsub -all -line "^h(.*-s 0 -d 29 -p cbr)" [read $fin] "d\\1"]
This will, however, hold all the data in memory at once.