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

mknod /tmp/oracle.pipe p

sqlplus / as sysdba << _EOF
set escape on

host nohup gzip -c < /tmp/oracle.pipe > /tmp/out1.gz &
spool /tmp/oracle.pipe
select * from employee;
spool off

_EOF

rm /tmp/oracle.pip

I need to insert a trailer at the end of the zipped file out1.gz ,
I can count the lines using

count=zcat out1.gz |wc -l

How do i insert the trailer

T5 (assuming count=5)

At the end of out1.gz without unzipping it.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 - >> original_file.gz

Method 2

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

Method 3

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x