I want to cat or merge 2 files together. Is it better to use exec cat or append?
Below is my cat script but doesnt seem to be working.
set infile [open "$dir/1.txt" w+]
puts $infile "set a 1"
puts $infile "set b 2"
exec /bin/cat "$dir/1.txt $dir2/ABC1.sh" > final.sh
close $infile
I bet "doesn't seem to be working" means you're only seeing the contents of $dir2/ABC1.sh
in final.sh
(Or maybe seeing part of what you're writing to $fir1/1.txt
, not all data). If so, you're running into buffering problems, trying to read $dir/1.txt
before closing or flushing infile
like you are. The data hasn't been written to the underlying file yet.
For concatenating multiple files into one, instead of relying on an external program like cat
(There might be problems with how you're quoting the arguments to exec
, too...), I'd use the various routines from the fileutil module from tcllib to do it in pure tcl
:
package require fileutil
fileutil::writeFile final.sh [fileutil::cat -- $dir/1.txt $dir2/ABC1.sh]
If you have big files and don't want to hold them all in memory (Or don't want to/can't install tcllib), chan copy
will also work:
set outfile [open final.sh w]
foreach file [list $dir/1.txt $dir2/ABC1.sh] {
set sourcefile [open $file r]
chan copy $sourcefile $outfile
close $sourcefile
}
close $outfile