Skip to main content
added 5 characters in body; edited title
Source Link
kyles
  • 33
  • 5

How can I add a property to json 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 a jsonan object named "object" that is inside a jsonconfiguration 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

How can I add a property to json 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 a json object named "object" that is inside a json 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

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
deleted 1 character in body
Source Link
kyles
  • 33
  • 5

I'm doing some text manipulation and I want to add a property (at the end) to a json object named "object" that is inside a json 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)

I'm doing some text manipulation and I want to add a property (at the end) to a json object named "object" that is inside a json 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)

I'm doing some text manipulation and I want to add a property (at the end) to a json object named "object" that is inside a json 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
added 12 characters in body
Source Link
kyles
  • 33
  • 5

I'm doing some text manipulation and I want to add a property (at the end) to a json object named "object" that is inside a json 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)

I'm doing some text manipulation and I want to add a property (at the end) to a json object named "object" that is inside a json 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 }   
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)

I'm doing some text manipulation and I want to add a property (at the end) to a json object named "object" that is inside a json 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)
added 70 characters in body
Source Link
kyles
  • 33
  • 5
Loading
added 42 characters in body
Source Link
kyles
  • 33
  • 5
Loading
Source Link
kyles
  • 33
  • 5
Loading