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
  • 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, 2018 at 9:27

1 Answer 1

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .