Skip to main content
2 of 3
added 47 characters in body
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

You could just do this with any version of awk so it'll work in any shell on every Unix box without needing to escape regexp metachars and without needing to repeat any parts of the string you're trying to match to be able to just print it as-is:

$ awk -v str='[etc]\ntransient = true' -v RS= '
    index("\n"$0"\n","\n"str"\n") { f=1 }
    { print }
    END { if (!f) print str }
' file
[composefs]
enabled = yes
[sysroot]
readonly = true
[etc]
transient = true
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60