使用jq打印具有指定值内容的json文件中的所有条目

时间:2023-01-21 15:04:22

I'm using jq for the first time. I have a large json file with entries like the one below. I would like to print the full content (like below) for all entries where the "file_name" contains 197407

我第一次使用jq。我有一个大的json文件,其条目如下所示。我想打印“file_name”包含197407的所有条目的完整内容(如下所示)

{
    "license": 5,
    "file_name": "COCO_train2014_000000057870.jpg",
    "coco_url": "http://mscoco.org/images/57870",
    "height": 480,
    "width": 640,
    "date_captured": "2013-11-14 16:28:13",
    "flickr_url": "http://farm4.staticflickr.com/3153/2970773875_164f0c0b83_z.jpg",
    "id": 57870
}

I tried the following, and got the following error:

我尝试了以下,并得到以下错误:

$ cat file1.json | jq -c '.file_name[]  | 
select(.file_name[]|contains("197407"))' 

jq: error: Cannot iterate over null

Question2: I would like to “sample” the contents of the file (it contains multiple formats of content), perhaps by printing every 1,000th entry. Can jq do this ?

问题2:我想“抽样”文件的内容(它包含多种格式的内容),也许是通过打印每1000个条目。 jq可以这样做吗?

2 个解决方案

#1


0  

Assuming your input file contains an array of the model you posted, you can match your records with :

假设您的输入文件包含您发布的模型的数组,您可以将您的记录与以下内容匹配:

cat file1.json | jq '.[] | select(.file_name | contains("197407"))'

In your statement .file_name[] means that you want to return each element of the array file_name

在您的语句中.file_name []表示您要返回数组file_name的每个元素

#2


0  

Re: Q2

回复:Q2

For efficiency, along the lines suggested by Jeff:

为了提高效率,请按照Jeff的建议:

.[range(0;length;1000)]

Or for fun:

或者为了好玩:

recurse(.[1000:]; . !=  []) | .[0]

#1


0  

Assuming your input file contains an array of the model you posted, you can match your records with :

假设您的输入文件包含您发布的模型的数组,您可以将您的记录与以下内容匹配:

cat file1.json | jq '.[] | select(.file_name | contains("197407"))'

In your statement .file_name[] means that you want to return each element of the array file_name

在您的语句中.file_name []表示您要返回数组file_name的每个元素

#2


0  

Re: Q2

回复:Q2

For efficiency, along the lines suggested by Jeff:

为了提高效率,请按照Jeff的建议:

.[range(0;length;1000)]

Or for fun:

或者为了好玩:

recurse(.[1000:]; . !=  []) | .[0]