I'm building a script to automate boilerplate. In it, I append a string:
"compile": "browserify js/main.js > ./dist/bundle.js -t babelify",
"watch": "watchify js/*.js -o ./dist/bundle.js -d",
With sed, I find the string "scripts" and append, as such.
sed '/"scripts"/a "compile": "browserify js/main.js > ./dist/bundle.js -t babelify",\n "watch": "watchify js/*.js -o ./dist/bundle.js -d",' package.json
So, the command has the syntax:
sed '/pattern/a' input
My problem is that this command is not mutating the input file, nor can I write the output to a file, e.g.,
sed '/pattern/a' input > output.txt
What am I doing wrong?
GOAL:
Input file (package.json):
{
"name": "torrent-search-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"torrent-search-api": "^2.1.4"
},
"devDependencies": {
"@babel/core": "^7.16.7",
"@babel/preset-env": "^7.16.8",
"babelify": "^10.0.0",
"browserify": "^17.0.0"
}
}
$ <command> package.json
Output:
cat package.json
{
"name": "torrent-search-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"compile": "browserify js/main.js > ./dist/bundle.js -t babelify",
"watch": "watchify js/*.js -o ./dist/bundle.js -d",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"torrent-search-api": "^2.1.4"
},
"devDependencies": {
"@babel/core": "^7.16.7",
"@babel/preset-env": "^7.16.8",
"babelify": "^10.0.0",
"browserify": "^17.0.0"
}
}
sedis simply the wrong tool for the job, as JSON is not a regular language. So, drop sed and use a JSON tool.jqis popular.