Is there a *Nix utility by which I could append content to a file (say, append the content of a template file into the end of a second file), only if the content isn't already available in the second file, partially or fully?

One could use this grep solution but I find the code there a bit complicated at the moment and would prefer a dedicated utility if there is one.


Assuming you'd like to append the contents of the file template to the file data only if no line from template already exists in data.

The command for testing whether a line in template is found in data is

grep -xF -f template data

(This is a reasonable thing to do only if template contains less than a few thousands of lines)

This uses the following options of grep:

  • -x, forcing testing complete lines, from start to finish of the line. Without this, we may get false positives if a line in template matches a line in data partially (for example, the line hello world matches hello world! partially).
  • -F, using string comparisons rather than regular expression matching. Without this option, grep would treat the lines in template as regular expressions, which may produce the wrong results if any pattern line contains characters that are special in regular expressions.
  • -f template, this makes grep use each line from template individually as patterns to match against each line in the data file.

Adding the -q option to this (to make grep quiet and to let it exit as soon as a match is found) allows us to use it in a simple test:

if ! grep -q -xF -f template data; then
    cat template >>data

The if statement body appends the template file to the end of the data file only if the grep command fails to find a match of any lines from template in data.

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.