【发布时间】:2021-03-29 13:16:32
【问题描述】:
我有一个这样的 json 文件:
[
{
"classname": "Test endpoint",
"name": "expect failure",
"failure_system_out": "expected 404 Not Found\nError in test endpoint\n\tat Test._assertStatus"
},
{
"classname": "Test inner functions",
"name": "expect failure",
"failure_system_out": "Example fo test\n\tExpect 4 and got 5"
}
]
如您所见,“failure_system_out”中的值是一个包含换行符 (\n) 和制表符 (\t) 的字符串。
我正在尝试读取文件,循环对象并使用以下代码打印它们:
jq -c '.[]' myfile.json | while read i; do
test_name=$(echo "$i" | jq -r .name)
system_error=$(echo "$i" | jq -r .failure_system_out)
printf "${system_error}"
done
问题在于,使用这种方法,printf 不会根据新行和制表符打印脚本,而是打印类似 expected 404 Not FoundnError in test endpointntat Test._assertStatus 的内容
基本上,我认为 jq -c 删除了 \ 字符,因此 printf 不能正常工作。
如何遍历存储在文件中的对象数组并保留用于格式化字符串的字符?
第一项的期望输出:
expected 404 Not Found
Error in test endpoint
at Test._assertStatus
第二个项目的期望输出:
Example fo test
Expect 4 and got 5
【问题讨论】:
-
请添加所需的输出!
-
在
jq设法看到扩展变量之前,内置的read命令删除了\` -
因为没有使用
-r标志。例如read -r -
@Jetchisel 你能写出适用于这个例子的代码吗?
-
Desired output:Example fo test...怎么了?