Enforce a line is present in a file only once
Enforcing a line to be present in a single occurrence in a file is not an easy process to automate if you can’t totally control the content of the file and need to let it editable by users.
The most powerful way to edit a file is to use the File content technique, because it allows making different changes on a file at once, and only modifying it after computing the resulting content.
The whole logic to edit a file so it contain only one occurrence of a line is:
Add the line, so it will be added if missing
Replace lines that looks almost like our line by the line
Delete all duplicated lines
With these 3 steps, you will end with one line! So, here is a small example: let’s say you
/etc/sysconfig/sysctl to contain line
You will need to create a directive based on the File content technique with the following content:
← Advanced file templating Enforce part of a line in a file →