Skip to main content
6 of 6
added 5 characters in body; edited title
kyles
  • 33
  • 5

How can I add a property in the end of the object (text manipulation)

I'm doing some text manipulation and I want to add a property (at the end) to an object named "object" that is inside a configuration file

e.g

.
.
object = {
   "one": "one",
   "two": "two",
}
.
.

Should become

.
.
object = {
   "one": "one",
   "two": "two",
   "three": "three",
}
.
.

This is my attempt, but i'm wondering if there is a cleaner way of doing things.

#find line for last property   
line=$(grep -n "$(awk "/object/{f=1;next} /}/{f=0} f" file.txt | tail -n 1)" file.txt | awk -F : '{ print $1 }')
content='\"three\" = \"three\"'
awk 'NR=='$((line + 1))' {print "\t '"$content"'"} 1' file.txt > file.temp && mv file.temp file.txt
kyles
  • 33
  • 5