I have a large (several GB) xml file that is missing a root element. I might use cat on ubuntu to wrap around a <root> element around the content.

How can I achieve this without having to extract and repack the content?

It works if I create separate prefix and postfix gz files, and concatenate them with cat. But could I do better, without having to create explicit pre-/postfix gz files? Can I achieve the same on the fly?

echo "<root>" | gzip > prefix.gz
echo "</root>" | gzip > postfix.gz
cat prefix.gz input.xml.gz postfix.gz > newfile.gz
  • 1
    since you're dealing with a multi-GB file, this question might be interesting to take a look at regarding the performance.
    – myrdd
    Apr 25 '18 at 9:27

You can use here strings and a succession of commands:

(gzip <<<"<root>"; cat input.xml.gz; gzip <<<"</root>") > newfile.gz

Here strings are described in What does <<< mean? and in the Bash manual.

  • gzip may refuse to compress short strings like that without -f. EDIT: No it doesn't, since it's from stdin.
    – Kusalananda
    Apr 25 '18 at 8:30
  • While I don't understand exactly, especially the <<<, it works as expected! Apr 25 '18 at 8:41
  • See this question or the Bash manual for a description of <<<. Apr 25 '18 at 9:03

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.