I have a file with .gz extension. When I try to read and print the file with following TCL commands I can't read the file even though I am able to see the contents in the VI editor.
I tried with the following TCL code:
set of [glob *.gz ]
set op [open "$of" r]
set file_data [read $op]
set data [split $file_data "\n"]
foreach line $data {
puts " $line"
}
In Tcl 8.6, you have built-in support for this so you can do:
The
zlib push gunzip
just attaches a suitable uncompressing transform to the channel.In 8.5 and before, you're best to read from a pipeline with
zcat
orgzcat
(depending on platform details:The down-side is that that's nowhere near as portable.
Read from Pipeline
Given a file named foo.gz, you could use something like the following:
This obviously relies on an external gzip utility. There may be a pure TCL solution, but this is fast and easy if your environment has a gzip executable.