【问题标题】:jq select substring from a columnjq 从列中选择子字符串
【发布时间】:2020-02-11 17:13:04
【问题描述】:

我的文件 test.json 包含以下内容:

{
  "result": [
    {
      "short_description": "The Net Energy Metering (NEM) Customer Application Database is used to track Customer Applications for a Net Energy Metering Interconnection Application. In almost all cases, the driver of these applications is prompted by the customer installing solar panels on the roof or his or her residence. There are numerous steps required in order to process the NEM applications. The Net Energy Metering Database facilitates this process and allows ACME to keep track of the current status of each customer application.",
      "managed_by": "Bill Anderson",
      "u_storm_impact_flag": "",
      "business_criticality": "Medium",
      "u_compliance_flag": "",
      "active": "true",
      "emergency_tier": "Tier 3"
    }
  ]
}

我可以成功地做到这一点:

cat test.json|jq -r '.result[].short_description'

The Net Energy Metering (NEM) Customer Application Database is used to track Customer Applications for a Net Energy Metering Interconnection Application. In almost all cases, the driver of these applications is prompted by the customer installing solar panels on the roof or his or her residence. There are numerous steps required in order to process the NEM applications. The Net Energy Metering Database facilitates this process and allows PHI to keep track of the current status of each customer application.

但是,如果我只是尝试提取“short_description”列的前 250 个字符,我会收到以下错误:

cat test.json|jq -r '.result[0:249].short_description'

jq: error (at <stdin>:13): Cannot index array with string "short_description"

你能告诉我我做错了什么吗?

谢谢!

【问题讨论】:

    标签: arrays json jq substr


    【解决方案1】:

    对“short_description”字符串应用子字符串过滤器:

    jq -r '.result[].short_description[:250]' test.json
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      • 2014-10-24
      相关资源
      最近更新 更多