Save the first N lines of a gzipped file to a new file (without unzipping)

Sun 07 February 2016

I needed to write an import function in R for a certain data file. There was no description of the file format available, but I did have access to the files themselves. These were gzipped text files over 10G.

Rather than unzipping them first and then use head:

head -1000 input > output

I would rather not unzip them (entirely) to begin with. This is possible using vi(m).

vi oldfile
:1,100 w newfile

This just opens the file (vim supports reading straight from gzip) the second line you enter in the vim command line. This selects the lines from 1 to 100 (in our case) and writes them to a new file called newfile.