根据jq教程,第4.1节末尾,jq有一个紧凑的表示法,其中可以跳过|操作符(以及它周围的空格)。
在SQuAD 2.0devset (下载链接)上,以下表达式可以工作:
$> cat src/dev-v2.0.json | jq ".data" | jq ".[]" | jq ".paragraphs" | jq ".[]" | jq ".qas" | jq ".[]" | jq ".question" | head
$> cat src/dev-v2.0.json | jq ".data | .[] | .paragraphs | .[] | .qas | .[] | .question" | head但是,“简明”符号并不是:
$> cat src/dev-v2.0.json | jq ".data.[].paragraphs.[].qas.[].question" | head
jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.data.[].paragraphs.[].qas.[].question
jq: 1 compile error正如错误提示的那样,这可能是一个编码问题,但是使用表达式周围的引号,这似乎不太可能。因此,我跟随jq建议在StackOverflow上问这个问题:我在这里遗漏了什么?
我使用的是Ubuntu20.04.3 LTS,jq-1.6,bash5.0.17(1)。
发布于 2022-04-09 16:08:44
.[]在上下文.中访问数组(或对象)的成员。.data | .[]在上下文.中访问对象的一个名为data的字段。通过将其传递到下一个筛选器,所访问的字段将成为新的上下文.,因此.[]在该上下文中访问数组(或对象)的成员。.data[]在上下文.中访问称为对象的data字段中的数组(或对象)的成员。jq ".data[].paragraphs[].qas[].question" dev-v2.0.json | head"In what country is Normandy located?"
"When were the Normans in Normandy?"
"From which countries did the Norse originate?"
"Who was the Norse leader?"
"What century did the Normans first gain their separate identity?"
"Who gave their name to Normandy in the 1000's and 1100's"
"What is France a region of?"
"Who did King Charles III swear fealty to?"
"When did the Frankish identity emerge?"
"Who was the duke in the battle of Hastings?"发布于 2022-04-09 16:12:02
.x|.[]|.y的有效缩写形式是.x[].y。
https://stackoverflow.com/questions/71809695
复制相似问题