Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read a gzip file line by line?

Tags:

file-io

ruby

gzip

I have a gzip file and currently I read it like this:

infile = open("file.log.gz")
gz = Zlib::GzipReader.new(infile)
output = gz.read
puts result

I think this converts the file to a string, but I would like to read it line by line.

What I want to accomplish is that the file has some warning messages with some garbage, I want to grep those warning messages and then write them to another file. But, some warning messages are repeated so I have to make sure that i only grep them once. Hence line by line reading would help me.

like image 482
infinitloop Avatar asked Dec 30 '11 21:12

infinitloop


People also ask

How do I extract a compressed gzip file?

You can unzip GZ files in Linux by adding the -d flag to the Gzip/Gunzip command. All the same flags we used above can be applied. The GZ file will be removed by default after we uncompressed it unless we use the -k flag. Below we will unzip the GZ files we compressed in the same directory.

What program opens gzip files?

Launch WinZip from your start menu or Desktop shortcut. Open the compressed file by clicking File > Open. If your system has the compressed file extension associated with WinZip program, just double-click on the file.

What is the difference between GZ and gzip?

The only difference is the name itself. GZIP is the file format, and GZ is the file extension used for GZIP compressed files.


2 Answers

You should be able to simply loop over the gzip reader like you do with regular streams (according to the docs)

infile = open("file.log.gz")
gz = Zlib::GzipReader.new(infile)
gz.each_line do |line|
  puts line
end
like image 72
Tigraine Avatar answered Oct 13 '22 00:10

Tigraine


Try this:

infile = open("file.log.gz")
gz = Zlib::GzipReader.new(infile)
while output = gz.gets
  puts output
end
like image 43
Sergio Tulentsev Avatar answered Oct 13 '22 00:10

Sergio Tulentsev