jq : 在字段中生成 UUID [英] jq : Generate UUID in field
问题描述
我需要使用 UUID 唯一地标记记录(用于关联 ID).我看不到通过选项直接执行此操作的方法,这可能吗?如果没有,是否有某种解决方法可以做到这一点?
I have a requirement to tag records uniquely with UUIDs (for a correlation id). I cant see a direct way to do this via the options, is such a thing possible? If not, is there some kind of workaround that might be able to do this?
甚至可以在 jq 中生成随机数或字符串吗?
Is it even possible to generate a random number or string in jq?
推荐答案
jq 目前不支持 UUID 生成,所以最好的办法是将 UUID 提供给 jq,例如沿着这些路线:
jq currently has no support for UUID generation, so your best bet would be to feed UUIDs in to jq, e.g. along these lines:
ruby -e 'require "securerandom"; p SecureRandom.uuid' | jq '{uuid: .}'
{
"uuid": "5657dd65-a495-4487-9887-c7f0e01645c9"
}
遗憾的是,jq 的 PRNG 贡献尚未正式发布.有关用 jq 编写的 PRNG 生成器的示例,请参见例如罗塞塔代码:
The PRNG contributions for jq have unfortunately not yet made their way into an official release. For examples of PRNG generators written in jq, see e.g. rosettacode:
https://rosettacode.org/wiki/Linear_congruential_generator#jq
假设 uuid 生成器(例如 uuidgen)可用,您可以按以下方式使用 input
或 inputs
:
Assuming the availability of a uuid generator such as uuidgen, you could use input
or inputs
along the following lines:
jq -nR '[range(0;10) | input]' < <(while true; do uuidgen ; done)
(请注意,此处已避免使用操作系统管道.)
(Notice that an OS pipe has been avoided here.)
这篇关于jq : 在字段中生成 UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!