英文:
How to generate the following string?
问题
Sure, here's the translated content you requested:
从这个示例中,我想生成以下字符串 -
@types/lodash.camelcase@^4.3.6 @types/lodash.kebabcase@^4.1.6 @types/lodash.lowercase@^4.3.6 @types/lodash.upperfirst@^4.3.6 @types/mime@^2.0.3 @types/swagger-jsdoc@^6.0.1 async@^3.2.2 aws-arn@^1.0.1
英文:
Consider the following a snippet from my package.json
"dependencies": {
"@types/lodash.camelcase": "^4.3.6",
"@types/lodash.kebabcase": "^4.1.6",
"@types/lodash.lowercase": "^4.3.6",
"@types/lodash.upperfirst": "^4.3.6",
"@types/mime": "^2.0.3",
"@types/swagger-jsdoc": "^6.0.1",
"async": "^3.2.2",
"aws-arn": "^1.0.1",
}
From this, I want to generate the following string -
@types/lodash.camelcase@^4.3.6 @types/lodash.kebabcase@^4.1.6 @types/lodash.lowercase@^4.3.6 @types/lodash.upperfirst@^4.3.6 @types/mime@^2.0.3 @types/swagger-jsdoc@^6.0.1 async@^3.2.2 aws-arn@^1.0.1
Any idea how to accomplish this?
答案1
得分: 3
以下是翻译好的部分:
"jq"被标记为:创建有效的JSON,然后使用"to_entries"和"join"两次进行拆分:
jq -r '.dependencies | to_entries | map(join("@")) | join(" ")' package.json
英文:
As jq
is tagged: Make a valid JSON, then disassemble with to_entries
, and join
twice:
jq -r '.dependencies | to_entries | map(join("@")) | join(" ")' package.json
@types/lodash.camelcase@^4.3.6 @types/lodash.kebabcase@^4.1.6 @types/lodash.lowercase@^4.3.6 @types/lodash.upperfirst@^4.3.6 @types/mime@^2.0.3 @types/swagger-jsdoc@^6.0.1 async@^3.2.2 aws-arn@^1.0.1
答案2
得分: 1
假设实际上是有效的JSON,您可以使用[string interpolation](https://stedolan.github.io/jq/manual/#Stringinterpolation-\(foo))和[`join`](https://stedolan.github.io/jq/manual/#join(str)):
.dependencies | to_entries | map("(.key)@(.value)") | join(" ")
字符串内插法相对于两次使用join给您更多的灵活性。您的情况可能会有所不同,对于示例情况,使用join就足够了。
英文:
Assuming actually valid JSON, you can use string interpolation) and join
:
.dependencies | to_entries | map("\(.key)@\(.value)") | join(" ")
String interpolation gives you a little more flexibility compared to using join twice. YMMV, and for sample cases join will suffice.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论