使用jq
,我想通过正则表达式搜索模式,并用标签之类的东西包装匹配的字符串<div>
$ echo "\"This is a valid json file"\" | jq '. | gsub("valid";"how_to_refer_to_matches";"i") -
Run Code Online (Sandbox Code Playgroud)
如何引用第二个参数中的匹配结果gsub
?
如果匹配项超过 1 个怎么办?
我用来systemd-timer
定期运行一个使用网络服务的脚本。
问题是,在系统恢复或唤醒时,互联网连接不会立即启动,但计时器会被触发,因此脚本返回错误(如果服务等待几秒钟,脚本将正确运行,并且会出现无需将任务推迟到下次运行。)
1-如何使计时器(或与其关联的服务)等待网络连接可用?
2-当系统尚未上线时,如何使计时器(或服务)不调用脚本?
systemd services network-interface systemd-timer connectivity
jq
具有将数字转换为字符串或连接字符串的内置功能。
如何在 jq 中格式化类似于printf
填充(%4s)的字符串。
例如,如何强制 number 占用 10 个左对齐字符空间?
echo '{"title" : "A sample name", "number" : 1214}' | jq '(.title) + " " + (.number | tostring)'
使用jq
,我们如何根据某些允许列表/阻止列表中每个元素键的包含/排除从数组中选择 json 元素?
我想做一个不区分大小写的包含(所以允许名单/阻止名单大小写无关紧要)。
这是我尝试过的(未实施阻止列表):
allowlist='["happy", "good"]'
blocklist='["sad", "bad"]'
jq --argjson allowlist "$allowlist" \
--argjson blocklist "$blocklist" \
'.[]
| select(.my_key | ascii_downcase
| contains($allowlist[]))' \
<<< '[{"my_key": "neutral"}, {"my_key": "neutral good"},
{"my_key": "neutral bad"}, {"my_key": "good"},
{"my_key": "bad"}, {"my_key": "happy sad bad"},
{"my_key": "neutral happy sad"}]'
Run Code Online (Sandbox Code Playgroud)
预期输出:
{"my_key": "neutral good"}
{"my_key": "good"}
Run Code Online (Sandbox Code Playgroud) 如何监视sysfs
文件更改(如/sys/class/net/eth0/statistics/operstate
)并在内容更改时执行命令?
inotify
不起作用 sysfs
我想根据条件重新定位数组的元素(更改数组元素的索引)。我不知道如何将其翻译为 jq,它更像是一种函数式语言。
基本上我想对数组进行排序,但特定元素的相对位置应该保持不变。
for each element:
if element.role==master => record type
for each element:
if element.type == recorded type
reposition the element to be below its master of similar type
Run Code Online (Sandbox Code Playgroud)
我可以用一个例子更好地解释。考虑input.json
。如何将类型为“x”的所有非主元素移动到其主元素下方,而不改变相同类型的非主元素的相对位置。(忽略“num”参数。仅用于显示相对性)
输入.json
for each element:
if element.role==master => record type
for each element:
if element.type == recorded type
reposition the element to be below its master of similar type
Run Code Online (Sandbox Code Playgroud)
目标.json
[
{
"type": "A",
"role": "master"
},
{
"num": 1, …
Run Code Online (Sandbox Code Playgroud) jq ×4
algorithms ×1
async ×1
bash ×1
command-line ×1
connectivity ×1
filesystems ×1
filter ×1
inotify ×1
linux-kernel ×1
pcre ×1
printf ×1
programming ×1
services ×1
sort ×1
sysfs ×1
systemd ×1