我試圖用jq替換所有值爲*
(星號字符)的JSON對象。使用j替換多個JSON對象的所有值
這是我的package.json文件:
{
"name": "project",
"version": "0.0.1",
"description": "project-desc",
"main": "gulpfile.js",
"directories": {
"test": "tests"
},
"dependencies": {
"babel-polyfill": "^6.7.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"gulp-clean": "^0.3.2",
"jquery": "^2.1.4",
"lodash": "^4.0.0",
"moment": "^2.13.0",
"moment-timezone": "^0.5.0",
"radium": "^0.16.2",
"react": "^15.1.0",
"react-bootstrap-sweetalert": "^1.1.10",
"react-dom": "^15.1.0",
"react-timeago": "^2.2.1",
"sprintf": "^0.1.5",
"smoothscroll": "~0.2.2"
},
"devDependencies": {
"babel": "^6.3.26",
"babelify": "^7.2.0",
"browserify": "^12.0.1",
"console-stamp": "^0.2.0",
"estraverse-fb": "^1.3.1",
"gulp": "^3.9.0",
"gulp-concat": "^2.6.0",
"gulp-sass": "^2.1.1",
"gulp-sourcemaps": "^1.6.0",
"gulp-util": "^3.0.7",
"lodash": "^4.5.1",
"lodash.assign": "^3.2.0",
"lodash.isfunction": "^3.0.8",
"lodash.reduce": "^4.3.0",
"node-sass": "^3.4.2",
"react-bootstrap": "^0.29.4",
"react-intl": "^2.1.0",
"reactify": "^1.1.1",
"sweetalert": "^1.1.3",
"vinyl": "^1.1.0",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.4.0",
"jsx-to-string": "~0.2.11"
},
"optionalDependencies": {
"pkg-save": "~1.0.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "mygiturl"
},
"author": "authorname",
"license": "MIT"
}
我想用星號來代替dependencies
,devDependencies
和optionalDependencies
對象的所有值,像這樣(以下簡稱爲例):
...
"devDependencies": {
"babel": "*",
"babelify": "*",
"browserify": "*",
"console-stamp": "*",
...
下面是我正在使用的過濾器:
.dependencies, .devDependencies, .optionalDependencies | .foreach .[] as $item ([]; $item = "*")
通過命令行傳遞爲:
cat package.json | jq '.dependencies, .devDependencies, .optionalDependencies | .foreach .[] as $item ([]; $item = "*")'
它雖然返回一個錯誤,而不是期望的結果:
error: syntax error, unexpected '[', expecting IDENT
.dependencies, .devDependencies, .optionalDependencies | .foreach .[] as $item ([]; $item = "*")
^
1 compile error
不僅僅是作爲一個語法錯誤,我敢肯定,這只是操縱頂級項目,而不是每個鍵對象內的價值對,這是我打算做的。
參考和相關的問題:
- https://stedolan.github.io/jq/manual/#foreach
- https://stedolan.github.io/jq/manual/#map(x),map_values(x)
- Can't match regex with sed