I have a typical .json file for Chef, for example. servername.json
{
"name": "myserver123",
"chef_environment": "test",
"run_list": [
"role[base-pkg]",
"role[interesting_stuff]",
"role[user_apps]"
]
}
What I would like to do is use the "one liner" to add a new role after the last role found in the file. Since I never know which roles or how many of them are in the file, I decided to find the final closing bracket "]"and add a new role above this.
I tried the following:
tac servername.json | sed -i '0,/\]/a "role[My_New_Role]"'
thinking that it will find (now) the first β]" and add a new line after it. However, when I run cmd, it adds the string "role [My_New_Role]" 3 times. Twice before "]"and once in the right place after"]"
Questions:
1) 3 , "0" ?
2) AWK, Perl Python (2.7.5)? ?
3) regex lookahead/behind tac?
4) , , , ? ?