How to append a line in a zipped file without unzipping?

From man gzip you can read that gzipped files can simply be concatenated:

ADVANCED USAGE Multiple compressed files can be concatenated. In this case, gunzip will extract all members at once. For example:

        gzip -c file1  > foo.gz
        gzip -c file2 >> foo.gz

  Then

        gunzip -c foo

  is equivalent to

         cat file1 file2

This could also be done using cat for the gzipped files, e.g.:

seq 1 4 > A && gzip A
echo 5 > B && gzip B
#now 1 to 4 is in A.gz and 5 in B.gz, we want 1 to 5 in C.gz:
cat A.gz B.gz > C.gz && zcat C.gz
1
2
3
4
5
#or for appending B.gz to A.gz:
cat B.gz >> A.gz

For doing it without external file for you line to be appended, do as follows:

echo "this is the new line" | gzip - | cat - >> original_file.gz

How big is your employee table? Unless you have a million employees, and each record takes thousands of bytes, it's probably not even worth the bother of compressing the output.

Almost certainly not worth the bother of compressing it as the output file is being created - so why not just output to uncompressed plain text, run count=$(wc -l out1) ; echo "T$count" >> out1, and then compress it with gzip out1?

alternatively, just run some variation of select count(*) from employee before the spool off command.


I was wrestling with a similar challenge: appending a few lines to a compressed sql dump. My solution was based on answer from @Fiximan

echo 'append this string' | gzip >> out.gz

Tags:

Linux

Pipe

Gzip