Reputation: 8796
I have 100 files in a directory of type .json. In these files I have a following block in all files
"bind_dev": [
"eth0",
"bond0"
],
I want to remove this block from all the files with .json extension. How can this be done using linux shell?
{
"name": "wnx_production",
"description": "Wellnext",
"cookbook_versions": {
"fms-base": "= 1.0.49",
"fms-zabbix": "= 0.1.35",
"linuxad_auth": "= 0.2.75",
"itcs104_bundle": "= 0.3.23",
"policy_all_deep_security": "= 0.1.17",
"fms-pdns": "= 0.1.38",
"fms-yum": "= 0.1.17",
"tscm": "= 1.0.29",
"sudo": "= 2.7.10"
},
"json_class": "Chef::Environment",
"chef_type": "environment",
"default_attributes": {
"satellite-client": {
"capsule": {
"capsule_name": "dal13ammcaps01.imzcloud.ibmammsap.local"
}
},
"kickstart_ip": "146.89.142.214",
"tscm": {
"kickstart_ip": "146.89.142.214",
"bind_dev": [
"eth0",
"bond0"
],
"server_ip": "169.55.192.110",
"proxy_ip": "169.55.28.46",
"installer_url": "http://146.89.142.214/post_data/pkg/tivoliscm/"
},
"sudo": {
"include_sudoers_d": false,
"suroot_alias": "SUROOT"
},
"itcs104_bundle": {
"gem_source": {
"ssl_verify": false,
"clear": true
}
},
"sshd_config": {
"permitrootlogin": "without-password"
},
"chef-client-12": {
"chef-client-location": "http://146.89.142.214/post_data/pkg/chef"
}
}
}
Upvotes: 0
Views: 245
Reputation: 15246
First, I wouldn't use sed for this. Personally I'd write it in Perl, but that's not what you asked.
Second, test this carefully, and by carefully I mean take a backup of the whole setup first, then run it a few times and see that it's doing all of what you want, and only what you want. (Again, use Perl...but since you asked...)
sed -r -n '/^\s*"bind_dev"\s*:.*/,/],/d' *.json
That's simplistic. Better to add more detail to the regex, but sometimes too much will make it miss things, where not enough will make it remove things you didn't want lost.
c.f.: this guide to extended sed regexes, and good luck.
Upvotes: 0
Reputation: 361575
jq is the best way to manipulate JSON data from the shell. Here's how you could use it to process each file:
for file in *.json; do
jq 'del(.default_attributes.tscm.bind_dev)' "$file" > "$file.tmp" &&
mv "$file.tmp" "$file"
done
Upvotes: 2