【问题标题】:jq replace substring in valuejq 替换值中的子字符串
【发布时间】:2022-08-14 03:32:07
【问题描述】:

这是我卷曲的输出

{
  \"expand\": \"renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations\",
  \"id\": \"240937\",
  \"self\": \"https://placeholder.atlassian.net/rest/api/latest/issue/240937\",
  \"key\": \"placeholder-355\",
  \"fields\": {
    \"description\": \"We need the following page layout changes made (for all user profiles unless specified in front of the item):\\n\\n# Display {{TestAccount__c}} field on the account object underneath the *Pricing Group* field\\n# Display {{blng__LegalEntity__c}} field that sits on {{Order Product}} to be visible on *Order* as well as *Contract* objects _(position doesn’t matter). Read only is fine as long as that works on SF reports if we need to bring this attribute in_\\n# Display {{blng__LegalEntity__c}} field that sits on *Invoice Lines* to be visible on the *Invoice* object _(position doesn’t matter). Read only is fine as long as that works on SF reports if we need to bring this attribute in_\\n# Display ‘*Notes*’ section on the credit request page - copy the one that exists on the contract object e.g. (sample below)\\n!image-20220714-021135.png|width=50%!\\n# -Display- *-Agent-* -field on contract object to enable users to see if account has an agent or not- → already in production (not needed)\\n# Change default options for *tasks* - the default subjects are:\\n#* Call\\n#* Send Letter\\n#* Send Quote\\n#* Other\\n# Need to update to the following:\\n#* Call\\n#* Video Call\\n#* Face to Face Meeting\\n# Add in *Contact hierarchy* functionality in Salesforce\\n## Anyone can update the ‘Reports to’ field\\n## Show the following fields on the hierarchy page\"
  }
}

随着 jq 我得到 json

 jq --raw-output \'.fields.description\' jira-story.json

结果:

# Display *Pricing Group* field
# Display *Contract*
# Change default options for *tasks* - the default subjects are:
#* Call
#* Send Letter
#* Send Quote
#* Other
# Need to update to the following:
#* Call
#* Video Call
#* Face to Face Meeting
# Add in *Contact hierarchy* 
## Anyone can update the ‘Reports to’ field
## Show the following fields on the hierarchy page

我希望它很好地格式化为

### Display *Pricing Group* field
### Display *Contract*
### Change default options for *tasks* - the default subjects are:
- Call
- Send Letter
- Send Quote
- Other
### Need to update to the following:
- Call
- Video Call
- Face to Face Meeting
### Add in *Contact hierarchy* 
- Anyone can update the ‘Reports to’ field
- Show the following fields on the hierarchy page

如何在输出之前替换 jq 中的值? \"#*\" 到 \"-\" 或 \"##\" 到 \"-\" 和 \"#\" 到 \"###\"

  • .fields.description | gsub(\"\\n#[#*]\";\"\\n-\") | gsub(\"\\n# \",\"\\n### \") ?

标签: bash jq


【解决方案1】:

您不一定需要在 jq 中执行替换,您可以将输出通过管道传输到 sed:

jq -r '.fields.description' | sed 's/^#[#*]/-/;s/^#/###/'

如果你真的必须用一个 jq 表达式来做,那么用户 jhnccomments 中写道:

.fields.description | gsub("\n#[#*]";"\n-") | gsub("\n# ","\n### ")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 2013-07-23
    • 2015-12-08
    相关资源
    最近更新 更多