read first line from .gz compressed file without decompressing entire file
Piping zcat
’s output to head -n 1
will decompress a small amount of data, guaranteed to be enough to show the first line, but typically no more than a few buffer-fulls (96 KiB in my experiments):
zcat logfile.gz | head -n 1
Once head
has finished reading one line, it closes its input, which closes the pipe, and zcat
stops after receiving a SIGPIPE
(which happens when it next tries to write into the closed pipe). You can see this by running
(zcat logfile.gz; echo $? >&2) | head -n 1
This will show that zcat
exits with code 141, which indicates it stopped because of a SIGPIPE
(13 + 128).
You can add more post-processing, e.g. with AWK, to only extract the date:
zcat logfile.gz | awk '{ print $1; exit }'
(On macOS you might need to use gzcat
rather than zcat
to handle gzipped files.)
You could limit the amount of data you feed to zcat
(or gzip -dc
), then ask for the first line:
head -c 1000 logfile.gz | zcat 2>/dev/null | head -1 | read logdate otherstuff
Adjust the 1000
if that doesn't capture enough data to get the entire first line.
To just match a date from the 1st line of a zipped file - zgrep
solution:
zgrep -m1 -o '^[^[:space:]]*' logfile.gz
This will output the first YYYY-MM-DD
for you.