Issue
I would like to delete a line containing "electron-inspector": "0.1.4",
from .package.json
The following is not working.
sed -i '' -e 'electron-inspector' ./package.json
What do I have to change?
Solution
You could do this, but sed in not a JSON parser !
sed -i '' -e '/electron-inspector/d' file
(-i ''
is required for macOS's sed)
Imagine a simple (inline) case like this :
{"foo": 123, "electron-inspector": "0.1.4", "bar": 42 }
BOOOM ! So :
The proper way, using jq :
jq -r 'del(.["electron-inspector"])' file.json > _.json && mv _.json file.json
The dash -
is a special case for jq
If instead you had a simple case, the command would be :
jq 'del(.electroninspector)' file.json
Check JQ
Another solution using nodejs and... javascript :
cat file.json
{"foo": 123, "electron-inspector": "0.1.4", "bar": 42 }
Code :
$ node<<EOF > _.json && mv _.json file.json
var o = $(< file.json);
delete o["electron-inspector"];
console.log(JSON.stringify(o, null, 4));
EOF
Edited file :
{
"foo": 123,
"bar": 42
}
Last but not least, to edit the file properly using sponge
:
Instead of : > x.json && mv x.json file.json
, we can do :
command ...... file.json | sponge file.json
You need moreutils installed.
Answered By - Gilles Quenot Answer Checked By - Senaida (WPSolving Volunteer)