Skip to main content
added 256 characters in body
Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

If it's to insert "three": "three", before the first } line that follows a object = { line, then that could be:

sed '
/^object = {$/,/^}$/{
  /^}$/i\
   "three": "three",
}'

Or same with awk:

awk '$0 == "object = {", $0 == "}" {
       if ($0 == "}") print "   \"three\": \"three\""
     }
     {print}'

Or:

awk '$0 == "object = {", $0 == "}" && $0 = "   \"three\": \"three\"\n" $0 {};1'

Relying on the fact that that $0 = ... assignment will always return true as guaranteed to be neither the empty string nor any representation of zero.

If it's to insert "three": "three", before the first } line that follows a object = { line, then that could be:

sed '
/^object = {$/,/^}$/{
  /^}$/i\
   "three": "three",
}'

Or same with awk:

awk '$0 == "object = {", $0 == "}" {
       if ($0 == "}") print "   \"three\": \"three\""
     }
     {print}'

If it's to insert "three": "three", before the first } line that follows a object = { line, then that could be:

sed '
/^object = {$/,/^}$/{
  /^}$/i\
   "three": "three",
}'

Or same with awk:

awk '$0 == "object = {", $0 == "}" {
       if ($0 == "}") print "   \"three\": \"three\""
     }
     {print}'

Or:

awk '$0 == "object = {", $0 == "}" && $0 = "   \"three\": \"three\"\n" $0 {};1'

Relying on the fact that that $0 = ... assignment will always return true as guaranteed to be neither the empty string nor any representation of zero.

Source Link
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

If it's to insert "three": "three", before the first } line that follows a object = { line, then that could be:

sed '
/^object = {$/,/^}$/{
  /^}$/i\
   "three": "three",
}'

Or same with awk:

awk '$0 == "object = {", $0 == "}" {
       if ($0 == "}") print "   \"three\": \"three\""
     }
     {print}'