106

How to get the first few lines from a gziped file ? I tried zcat, but its throwing an error

zcat CONN.20111109.0057.gz|head
CONN.20111109.0057.gz.Z: A file or directory in the path name does not exist.
jamessan
  • 41,569
  • 8
  • 85
  • 85
Govind Kailas
  • 2,645
  • 5
  • 22
  • 24

6 Answers6

180

zcat(1) can be supplied by either compress(1) or by gzip(1). On your system, it appears to be compress(1) -- it is looking for a file with a .Z extension.

Switch to gzip -cd in place of zcat and your command should work fine:

 gzip -cd CONN.20111109.0057.gz | head

Explanation

   -c --stdout --to-stdout
          Write output on standard output; keep original files unchanged.  If there are several input files, the output consists of a sequence of independently compressed members. To obtain better compression, concatenate all input files before compressing
          them.

   -d --decompress --uncompress
          Decompress.
Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • 8
    BTW, if you are sitting with a *.tar.gz, this will help you out: `tar -xzOf some_huge_file.tar.gz | head` – demaniak Feb 25 '13 at 13:57
  • Old thread but this produces a broken pipe with exit status 1 with big gz files. Any clean workaround? – kaligne Sep 15 '17 at 11:55
  • 2
    Best and easiest workaround I've found so far: use `zless file.gz | head`. `zmore` still leaves you with broken pipe. `zless` seems to be the way to go. – kaligne Sep 15 '17 at 12:04
  • zless doesn't exit... at least not on my large file. I'm still looking for a way do do this without broken pipe errors... – Freek May 30 '18 at 11:55
18

On a mac you need to use the < with zcat:

zcat < CONN.20111109.0057.gz|head

punkrockpolly
  • 9,270
  • 6
  • 36
  • 37
16

On some systems (e.g., Mac), you need to use gzcat.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
4

If a continuous range of lines needs be, one option might be:

gunzip -c file.gz | sed -n '5,10p;11q' > subFile

where the lines between 5th and 10th lines (both inclusive) of file.gz are extracted into a new subFile. For sed options, refer to the manual.

If every, say, 5th line is required:

gunzip -c file.gz | sed -n '1~5p;6q' > subFile

which extracts the 1st line and jumps over 4 lines and picks the 5th line and so on.

Herpes Free Engineer
  • 2,425
  • 2
  • 27
  • 34
4

If you want to use zcat, this will show the first 10 rows

zcat your_filename.gz | head

Let's say you want the 16 first row

zcat your_filename.gz | head -n 16
RCchelsie
  • 111
  • 6
0

This awk snippet will let you show not only the first few lines - but a range you can specify. It will also add line numbers which i needed for debugging an error message pointing to a certain line way down in a gzipped file.

gunzip -c file.gz | awk -v from=10 -v to=20 'NR>=from { print NR,$0; if (NR>=to) exit 1}'

Here is the awk snippet used in the one liner above. In awk NR is a built-in variable (Number of records found so far) which usually is equivalent to a line number. the from and to variable are picked up from the command line via the -v options.

NR>=from {
   print NR,$0; 
   if (NR>=to) 
     exit 1
}
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186