You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写Shell脚本将文本文件中的试题转换为JSON格式

从文本试题文件生成指定JSON结构的Shell脚本方案

需求说明

现有一个input.txt文件,内容是按固定格式排列的试题,每道题以空行分隔,格式为:编号+问题、四个选项(a-d开头)、答案行。需要编写Shell脚本将其转换为包含每道题详细信息的JSON数组。

输入文件示例(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

期望输出的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"
 }
]

注:你提供的示例输出末尾有语法错误,已修正为正确的JSON闭合格式

你尝试的脚本(未成功)

#!/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

可行解决方案:使用AWK处理

AWK天生适合按段落(空行分隔)处理文本,以下脚本可以精准解析每道题并生成符合要求的JSON:

#!/usr/bin/awk -f

BEGIN {
    print "["
    first_entry = 1
}

# 空行作为段落分隔符,处理当前段落的数据
/^$/ {
    if (question != "") {
        if (!first_entry) {
            print ","
        }
        printf " {\n  \"question\": \"%s\",\n  \"a\": \"%s\",\n  \"b\": \"%s\",\n  \"c\": \"%s\",\n  \"d\": \"%s\",\n  \"answer\": \"%s\"\n }", question, opt_a, opt_b, opt_c, opt_d, answer
        first_entry = 0
        # 重置变量,准备下一道题
        question = opt_a = opt_b = opt_c = opt_d = answer = ""
    }
    next
}

# 匹配编号开头的问题行
/^[0-9]+)/ {
    question = substr($0, index($0, " ") + 1)
    next
}

# 匹配选项行
/^a)/ {
    opt_a = substr($0, index($0, " ") + 1)
    next
}
/^b)/ {
    opt_b = substr($0, index($0, " ") + 1)
    next
}
/^c)/ {
    opt_c = substr($0, index($0, " ") + 1)
    next
}
/^d)/ {
    opt_d = substr($0, index($0, " ") + 1)
    next
}

# 匹配答案行
/^Answer:/ {
    answer = substr($0, index($0, ")") + 2)
    next
}

# 处理文件末尾的最后一道题(如果没有空行结尾)
END {
    if (question != "") {
        if (!first_entry) {
            print ","
        }
        printf " {\n  \"question\": \"%s\",\n  \"a\": \"%s\",\n  \"b\": \"%s\",\n  \"c\": \"%s\",\n  \"d\": \"%s\",\n  \"answer\": \"%s\"\n }", question, opt_a, opt_b, opt_c, opt_d, answer
    }
    print "\n]"
}

使用方法

  1. 将上述代码保存为convert_to_json.awk
  2. 赋予执行权限:chmod +x convert_to_json.awk
  3. 运行脚本:./convert_to_json.awk input.txt > output.json

脚本说明

  • BEGIN块:输出JSON数组的开头标记[,并初始化标记变量判断是否为第一个条目。
  • 空行处理:当遇到空行时,将当前收集的试题数据格式化为JSON对象输出,同时处理条目间的逗号分隔。
  • 匹配各行:分别捕获问题行、四个选项行、答案行,提取其中的核心文本内容。
  • END块:处理文件末尾没有空行结尾的最后一道题,输出JSON数组的闭合标记]。

内容的提问来源于stack exchange,提问作者William Brown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 15:43:28