【发布时间】:2017-12-13 05:15:00
【问题描述】:
我正在尝试在 jq 中对数组进行切片,其中结束索引作为参数从 shell (bash) 传递:
end_index=7
cat obj.json | jq --arg eidx $end_index, '.arr[0:$eidx]'
当索引被硬编码时,这可以正常工作
cat obj.json | jq '.arr[0:7]'
但在顶部的示例中,我收到一条错误消息
jq: error (at <stdin>:0): Start and end indices of an array slice must be numbers
我怀疑这可能与 jq 如何处理切片运算符 [:] 中的变量替换有关,但我没有尝试解决问题,例如通过将变量名括在花括号 .arr[0:${eidx}] 中,已经奏效了。
【问题讨论】:
标签: json bash casting command-line-arguments jq