Benjamin Dell Benjamin Dell - 1 year ago 87
Linux Question

Appending a line to a file ONLY if it doesnt already exist. Using Sed?

I need to add the following line to the end of a config file:

include "/configs/projectname.conf"

to a file called

I am looking into using
to do this, but I can't work out how.

How would I only insert it if the line doesn't already exist?

Answer Source

Just keep it simple :)

grep + echo should suffice:

grep -q -F 'include "/configs/projectname.conf"' || echo 'include "/configs/projectname.conf"' >>

Edit: incorporated @Cerin suggestion.

NOTE: for "editors" changing my answer: if you have an alternative solution give your own answer! Editing is intended for clarifying, formatting, better English and so on! Don't change the answer given by others! (thank you)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download