Sample Header Ad - 728x90

Sed - Help Matching with Multi-Line Match

3 votes
3 answers
242 views
I am fairly new to regexes and it appears I am missing something in my understanding. I have the following file, and I am trying to insert a value if it is missing. The file looks like
[composefs]
enabled = yes
[sysroot]
readonly = true
I would like to insert the following only if it is absent
[etc]
transient = true
My sed search and replace looks like sed -zi '/^[etc]\n^transient = true/!s/$/[etc]\ntransient = true/' file When I run sed it adds the value as expected, but it keeps adding the value un subsequent runs so I assume my matching is not working. How can I do this correctly?
Asked by adam (33 rep)
May 10, 2025, 12:07 PM
Last activity: May 14, 2025, 12:12 PM