将每行多个 { } 之间的文本复制到文本文件中

将每行多个 { } 之间的文本复制到文本文件中

我需要一个解决方案来通过以下方式提取文本:

输入.txt

"description": "{A, 0}, {B, 1200}, {C, 543}",

(等等)

这是来自一个更大的 JSON 文件:

{
  "cover_path": "n/a",
  "description": "{A, 0}, {B, 1200}, {C, 543}",
  "duration": 541234,
  "title": "16.05.2013 - Title"
}

预期输出:

A.txt

A, 0

B.txt

B, 1200

C.txt

C, 543

答案1

#! /usr/bin/env bash

description=$(cat "large.json" | jq -r ".description")
readarray -d "{" -t desc_array <<<"${description}"
for part in "${desc_array[@]}"; do
    [[ -z "${part}" ]] && continue
    part="${part/%$'\n'/}"
    part="${part/\}/}"
    part="${part// /}"
    part="${part/%,/}"
    IFS="," read -r basename content <<<"${part}"
    printf "%s, %s" "${basename}" "${content}" > "${basename}.txt"
done

相关内容