使用jq数组切片但遇到数组错误
我在我的Attendes.txt上有这样的例子。现在我想过滤名称Jasmine,直到标记,这样它将只输出名称使用jq数组切片但遇到数组错误,jq,Jq,我在我的Attendes.txt上有这样的例子。现在我想过滤名称Jasmine,直到标记,这样它将只输出名称 { "Attendees": [ { "Name": "Joseph", "CreationDate": "2019-08-13T08:35:17+00:00" }, {
{
"Attendees": [
{
"Name": "Joseph",
"CreationDate": "2019-08-13T08:35:17+00:00"
},
{
"Name": "Jasmine",
"CreationDate": "2019-01-07T03:24:01+00:00"
},
{
"Name": "Rebecca",
"CreationDate": "2020-05-19T13:20:58+00:00"
},
{
"Name": "Mark",
"CreationDate": "2019-01-30T15:09:39+00:00"
}
我可以使用以下命令:
jq -r '.Attendees[1,2,3].Name' < attendees.txt
jq-r'。与会者[1,2,3]。名称“
但是如果我有一千行名字,而我只想过滤300-500个呢?如何正确过滤范围并仅输出名称?我输入了下面的命令,它说不能用字符串“Name”索引数组
jq-r'。与会者[300:500]。名称“
或者类似地,如果我需要使用range过滤上述名称,我会遇到相同的错误
jq -r '.Attendees[1:3].Name' < attendees.txt
jq-r'。与会者[1:3]。名称“
该操作返回一个数组,您仍然需要从中选择子对象值
jq-r'。与会者[1:4][]姓名'
我想过滤Jasmine这个名字直到Mark
因此,首先找到它们各自的指数:
.Attendees
| map(.Name)
| index("Jasmine") as $first
| index("Mark") as $last
| select($first and $last)) # if both must be present
| .[$first: $last+1][]
这里有一个备选方案,(a)以不同的方式处理边缘情况,(b)说明了如何避免构建第二个数组:
.Attendees | map(.Name)
| (index("Jasmine") // 0) as $first
| (index("Mark") // length) as $last
| .[ range($first; $last+1) ]
为了避免构建新数组,您可以使用
range
:jq-r'.attendes[range(1;4)].Name'
.Attendees | map(.Name)
| (index("Jasmine") // 0) as $first
| (index("Mark") // length) as $last
| .[ range($first; $last+1) ]