2016-08-23 10 views
1

jqを使用して、JSONオブジェクトのすべての値を*(アスタリスク文字)に置き換えようとしています。複数のJSONオブジェクトのすべての値をjqに置き換えよう

これは私のpackage.jsonファイルである:私はそう(以下略し例)のように、アスタリスクでdependenciesdevDependenciesoptionalDependenciesオブジェクトのすべての値を置き換えたい

{ 
    "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" 
} 

... 
"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 

もっと、私はそれが唯一のトップレベルのアイテムではなく、各キーの操作だと確信しています私がしようとしているオブジェクト内の値のペア。

参考文献や関連質問:

答えて

2

タスクを達成するための最も直接的な方法は、おそらく次のフィルタを使用することです:

繰り返しを避けるために

(.dependencies, .devDependencies, .optionalDependencies) |= map_values("*")

4

あなたはカンマ演算子を利用することによって一挙にあるすべてを更新することができます。すべての値を"*"に更新するには、これを行うことができます。

.["dependencies", "devDependencies", "optionalDependencies"] |= map_values("*") 
関連する問題