mardi 1 septembre 2020

random/sequential numbers in JQ (or workaround)

IIUC jq does not support random numbers at the moment. I have script producing lines, where each line is JSON. I need to pipe it to jq to transform the data, and output pipe yet further. The problem is, that I need as a part of jq transformation to set some field to some random/sequential value. And since the jq is invoked just once and processes streamed data, I cannot supply random value from bash, as it would be not random, but always the same.

Is there a workaround/solution for this?

Comment: maybe I could use some constant in place of random value, and then replace externally, but if I pipe jq output even to: while read R;do ...;done then the data are available only after whole input is processed, which is not possible, every line has to be processed asap. Maybe this is just a incorrect bash usage? If so, do you know how to fix that? EDIT: ok, so that is solved by --unbuffered parameter. So this is one workaroudnd. If you know better, please share.

example of invocation of my nasty workaround:

cat | jq --unbuffered -c '.random="PLACEHOLDER"' | while read R; do echo $R | sed "s/PLACEHOLDER/$RANDOM/";done | sed "s/^/anotherScriptMock/"

and start entering valid jsons like {}




Aucun commentaire:

Enregistrer un commentaire