Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append one line at the top of huge gzip file

I have a huge gzip file (~400MB). I want to append one line of text at the BEGINNING of the file.

I was thinking of creating a gzip file with the header line and then using zcat to combine header file and log file. Just wanted to check if there is a better/elegant/efficient way to do it.

like image 209
aadarshsg Avatar asked Dec 11 '22 22:12

aadarshsg


1 Answers

two gzipped files concatenated to a single file is actually a valid gz file.

Try it.

Gzip your first, single line that you want to prepend, then cat the two to a third.

print "My newline" | gzip -c > /tmp/smallzip.gz
cat /tmp/smallzip.gz mybigfile.gz > newbigfile.gz 

That would save the time and cpu of unzipping the big gz file, prepending your line and rezipping, which would be:

(
    echo "My newline"
    zcat bigfile.gz
) | gzip -c > newbifile.gz 
like image 97
Abe Crabtree Avatar answered Jan 02 '23 15:01

Abe Crabtree