添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Im trying increment a global variable. The idea I had is do that by using global variable plus assignment.

content of input.json
{ "a" : 0, "b" : "text" }
{ "a" : 0, "b" : "text3" }
{ "a" : 0, "b" : "text3" }

cat input.json | jq -arg counter 0 '.a |= $counter++'

and have the output:
{ "a" : 1, "b" : "text" }
{ "a" : 2, "b" : "text3" }
{ "a" : 3, "b" : "text3" }

Is that possible to do that this way or there are other ways to get the same result?

other ideas:
cat input.json | jq -arg counter 0 '.a |= ($counter = $counter + 1)'

If your jq has foreach then a very efficient solution is possible using inputs and the -n command-line option. Otherwise you could use reduce with the -s command-line option.

If you have any further usage-related questions, please ask at stackoverflow.com with the jq tag:
https://stackoverflow.com/questions/tagged/jq

@pkoppstein thank you. I did a tour around stackoverflow and this example with foreach and inputs and the other one with reduce is explained here: https://stackoverflow.com/questions/34510742/how-do-i-create-an-incremental-index-with-jq/45525443#45525443 . The approach they use is for an array of objects.

In my case I have only a sequence of objects as in the example I sent. I couldnt find an example like the one im facing here.

I read it again and I got the idea. Your answer point me to the right solution and I would like to thank you for that.

I just had to understand that to be able to do this increment I need to use -s in order to make my input of objects to be an array and then work using reduce.

$ cat input.json
{ "a" : 0, "b" : "text" }
{ "a" : 0, "b" : "text3" }
{ "a" : 0, "b" : "text3" }
$ cat input.json | jq -s 'reduce range(0,length) as $i ( . ; .[$i].a = "\($i+1)") |.[]' -c
{"a":"1","b":"text"}
{"a":"2","b":"text3"}
{"a":"3","b":"text3"}

Im trying increment a global variable

You have to restructure your mind without global variables:

http://wiki.c2.com/?GlobalVariablesConsideredHarmful

This is an investment with future benefits in any language.

@gsilos wrote:

I just had to understand that to be able to do this increment I need to use -s in order to make my input of objects to be an array and then work using reduce.

I'm not sure how to interpret the above exactly, but for the avoidance of doubt, if your jq has inputs, then it will usually be better (more efficient) to use inputs with the -n option.

@fadado wrote:

You have to restructure your mind without global variables

Again for the avoidance of doubt, please note that the generic concerns about "global variables" simply do not apply in the present context, firstly because jq does not have "global variables" in the usual sense, and secondly because the question simply concerns how to operate over a stream of inputs.

As commented above, it is more efficient to use inputs with -n option than slurping the entire input. Here's a solution using foreach.

 $ jq -n 'foreach inputs as $x (0; .+1; $x+{a:.})' -c <<'EOF'
{"a":0,"b":"text"}
{"a":0,"b":"text3"}
{"a":0,"b":"text3"}
{"a":1,"b":"text"}
{"a":2,"b":"text3"}
{"a":3,"b":"text3"}