生成以下字符串的方法?

huangapple go评论72阅读模式
英文:

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

Demo

答案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.

huangapple
  • 本文由 发表于 2023年4月19日 14:38:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定