I have a typical .json file for Chef e.g. 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 a "one liner" to add a new role after the last role found in the file. Since I never know what roles or how many are in a file i thought to search for the final closing "]"
bracket and add the new role above that.
I tried the following:
tac servername.json | sed -i '0,/\]/a "role[My_New_Role]"'
thinking that this would find the (now) first "]" and add the new line after it. However when I run the cmd it adds the line "role[My_New_Role]" 3 times. Twice before the "]"
and once in the correct place after the "]"
Questions:
1) Why is the line added 3 times when the "0" says match once ?
2) Would this be better done using AWK, Perl or Python (2.7.5) ? How ?
3) Should I use regex lookahead/behind instead of tac ?
4) Since I will need to process the file again to add a comma to the second to last role perhapes my whole approach is wrong ? What would be a better approach ?