我有一个包含内容的文本文件:
//input.txt
1) What is the first month of the year?
a) March
b) February
c) January
d) December
Answer: c) January
2) What is the last month of the year?
a) July
b) December
c) August
d) May
Answer: b) December
Run Code Online (Sandbox Code Playgroud)
我想编写一个 shell 脚本,循环遍历此文件 input.txt (其中包含更多具有相同格式的内容)并生成类似于以下 JSON 的输出
[
{
"question": "What is the first month of the year?",
"a": "March",
"b": "February",
"c": "January",
"d": "December",
"answer": "January",
},
{
"question": "What is the last month of the year?",
"a": "July",
"b": "December",
"c": "August",
"d": "May",
"answer": "December",
},
[
Run Code Online (Sandbox Code Playgroud)
我首先尝试编写一个 bash 脚本,循环遍历文件,并将由空行分隔的每一行放入大括号中,并将大括号中的每个项目放入引号中,并用逗号分隔,但事实并非如此在职的
#!/bin/bash
output=""
while read line; do
if [ -z "$line" ]; then
output+="}\n"
else
output+="\"${line}\","
if [ $(echo "$output" | tail -n 1) == "" ]; then
output+="{"
fi
fi
done < input.txt
output+="}"
echo "$output" > output.txt
Run Code Online (Sandbox Code Playgroud)
这是使用jq 的一种方法:
jq -R -s '
sub("\n+$"; "") |
split("\n\n") | map(
split("\n") | map(split(") ")) | [
{question: .[0][1]},
(.[1:-1][] | {(.[0]): .[1]}),
{answer: .[-1][1]}
] | add
)' input.txt
Run Code Online (Sandbox Code Playgroud)
尝试使用 Bash 生成正确的 JSON 会让您费尽心思。
首先,您的示例 JSON 输出不是正确的 JSON。,数组和映射中不支持尾随。所以你的例子需要是:
[{
"question": "What is the first month of the year?",
"a": "March",
"b": "February",
"c": "January",
"d": "December",
"answer": "January"
},
{
"question": "What is the last month of the year?",
"a": "July",
"b": "December",
"c": "August",
"d": "May",
"answer": "December"
}
]
Run Code Online (Sandbox Code Playgroud)
(注意,在每个"answer"或最后一个之后没有。您使用工具或jsonlint}检查有效的 JSON )
为了根据您的输入生成该数据,有许多 JSON 生成器工具。对我来说最简单的是 Ruby:
ruby -00 -r json -ne '
BEGIN{out=[]}
sub(/\A\d+\)\s+/,"question)")
sub(/Answer: [a-z]/,"answer")
out << $_.split(/\R/).map{|l| l.split(/[):]\s*/,2)}.to_h
END{puts JSON.pretty_generate(out)}' file
Run Code Online (Sandbox Code Playgroud)
印刷:
[
{
"question": "What is the first month of the year?",
"a": "March",
"b": "February",
"c": "January",
"d": "December",
"answer": "January"
},
{
"question": "What is the last month of the year?",
"a": "July",
"b": "December",
"c": "August",
"d": "May",
"answer": "December"
}
]
Run Code Online (Sandbox Code Playgroud)