Zet*_*tor 6 command-line bash printf text-formatting jq
jq具有将数字转换为字符串或连接字符串的内置功能。
如何在 jq 中格式化类似于printf填充(%4s)的字符串。
例如,如何强制 number 占用 10 个左对齐字符空间?
echo '{"title" : "A sample name", "number" : 1214}' | jq '(.title) + " " + (.number | tostring)'
dos*_*nak 21
jq可以使用引用字符串内的表达式\(foo)
在字符串内部,您可以将表达式放在反斜杠后面的括号内。无论表达式返回什么,都将被插入到字符串中。
jq '"The input was \(.), which is one less than \(.+1)"' <<< 42
Run Code Online (Sandbox Code Playgroud)
结果:
"The input was 42, which is one less than 43"
Run Code Online (Sandbox Code Playgroud)
jq没有printf。一种方法可能是;部分取自这里:
echo '{"title" : "A sample name", "number" : 1214}' |
jq '(.title) + " " +
(.number | tostring | (" " * (10 - length)) + .)'
Run Code Online (Sandbox Code Playgroud)
也许更好地添加为模块。
就我个人而言,我很快发现jq线条有些凌乱,如果做任何超出基础的事情,就会求助于 perl、python、php 或类似的东西。(但那是我:P)
例如使用 php:
echo '{"title" : "A sample name", "number" : 1214}' |
jq '(.title) + " " +
(.number | tostring | (" " * (10 - length)) + .)'
Run Code Online (Sandbox Code Playgroud)
(当然也会添加错误检查等。)
一种方法是不尝试在 中执行此操作jq,而是使用jq输出 shell 语句在 shell 中执行此操作:
eval "$(
jq -r -n '
{ "title": "A sample name", "number": 1214 } |
[ "printf", "%s %10s\\n", .title, .number ] | @sh'
)"
Run Code Online (Sandbox Code Playgroud)
或者,
eval "$(
printf '%s\n' '{ "title": "A sample name", "number": 1214 }' |
jq -r '[ "printf", "%s %10d\\n", .title, .number ] | @sh'
)"
Run Code Online (Sandbox Code Playgroud)
或者,
printf '%s\n' '{ "title": "A sample name", "number": 1214 }' |
{
eval "$(
jq -r '[ "printf", "%s %10s\\n", .title, .number ] | @sh'
)"
}
Run Code Online (Sandbox Code Playgroud)
该jq命令将输出
'printf' '%s %10d\n' 'A sample name' 1214
Run Code Online (Sandbox Code Playgroud)
使用@sh运算符安全地正确引用命令的每一位。评估时,这将输出
A sample name 1214
Run Code Online (Sandbox Code Playgroud)
类似的方法,但给你两个变量赋值:
A sample name 1214
Run Code Online (Sandbox Code Playgroud)
然后,您将在脚本中使用这些变量:
jq -r -n '
{ "title": "A sample name", "number": 1214 } |
@sh "title=\(.title)",
@sh "number=\(.number)"'
Run Code Online (Sandbox Code Playgroud)
对于已知数据很好的情况(例如,标题不能包含换行符),您可能会这样做
unset -v title number
eval "$(
jq -r -n '
{ "title": "A sample name", "number": 1214 } |
@sh "title=\(.title)",
@sh "number=\(.number)"'
)"
printf '%s %10s\n' "$title" "$number"
Run Code Online (Sandbox Code Playgroud)
也就是说,确保数据被引用,然后将其传递给printfshell(这将调用外部实用程序printf,而不是内置的 shell)。