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


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.

  • Do I understand you correctly: you want to add a line of text to a zipped file? – Fiximan Feb 22 '16 at 9:27
  • Yes without unzipping the file – Rishi Deorukhkar Feb 22 '16 at 9:30
  • 1
    i) count=zcat out1.gz |wc -l won't work, you need something like count=$(zcat out1.gz |wc -l); ii) zcat is unzipping the file, if you're willing to do that, why don't you wan to unzip? iii) Why don't you count the lines and add the number before compressing the file in the first place? – terdon Feb 22 '16 at 9:34
  • BTW, "to insert at the end" is called "to append", which is exactly what the accepted answer does. Maybe you should edit the title. – Dmitry Grigoryev Feb 22 '16 at 15:22

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


        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
#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
  • This may work for your simple example, as there's hardly anything to zip. Now try with more complex lines which contain overlapping substrings. – Murphy Feb 22 '16 at 9:40
  • Seemed to work with adding "Hello" toa cvs file of mine that was zipped from a size of 400kB to 100kB. But I do not know how the compression in gzip works and if if could corrupt anything. So far a few text files I tried were all fine. – Fiximan Feb 22 '16 at 9:54
  • Seems like this will indeed work; the blind spot was on my side: stackoverflow.com/questions/8005114/… – Murphy Feb 22 '16 at 10:01
  • @Murphy thanks for the clarification - I used this before and you got me worried about my previous data – Fiximan Feb 22 '16 at 10:02
  • It wasn't clear from your text if you've just made that solution up. I'm sorry for the downvote and would like to revert it, but unfortunately the vote is now locked until you edit the answer - another idiosyncrasy particular to U&L? – Murphy Feb 22 '16 at 10:06

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.

  • The table has 2 million records and uncompressed extract is about 2GB but have a space restriction hence need to compress it on the fly – Rishi Deorukhkar Feb 22 '16 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.