1

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 Answers1

3

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.

Stephen Kitt
  • 434,908