【问题标题】:Extract sentences from a json file using bash使用 bash 从 json 文件中提取句子
【发布时间】:2014-03-11 00:52:43
【问题描述】:

我有一个json文件,内容如下:

"1034280": {
    "transcript": ["1040560",
    "Ok, so what Akanksha is saying is that..."],
    "interaction": "Student Question"
},
"1041600": {
    "transcript": ["1044860",
    "this is also not correct because it will take some time."],
    "explain": "Describing&Interpreting"
},
"1046800": {
    "transcript": ["1050620",
    "So, what you have to do is, what is the closest to the answer?"],
    "question": "FocusingInformation"
},

我想提取转录句子并将它们连接起来。

例如。我希望输出为:

"Ok, so what Akanksha is saying is that..." "this is also not correct because it will take some time." "So, what you have to do is, what is the closest to the answer?"

【问题讨论】:

  • 如果您必须为此使用 bash(最好切换到具有原生 JSON 解析器的语言),jsawk 是您的朋友。
  • +1 对查尔斯的评论:ruby -rjson -e 'JSON.parse(STDIN.read).each_pair {|k,v| print %q{"%s" } % v["transcript"][1]}' 有效,如果示例文本被调整为有效的 JSON。
  • 您的json 无效。

标签: json bash sed awk grep


【解决方案1】:

这可能对你有用(GNU sed):

sed '/{/,+2{//,+1d;s/^\s*\|],\s*$//g;H;};$!d;x;s/\n//;y/\n/ /' file

【讨论】:

    【解决方案2】:

    注意事项

    • 您确实应该使用 cmets 中指示的 JSON 解析库
    • 这可能仅在您的数据与问题完全匹配时才有效
    • 由于您没有具体说明您尝试了什么,因此我将由您来解密 awk

    当输入数据在名为data的文件中时:

    awk -F"]," 'BEGIN { ORS="" } /"transcript":/ {p=1} NF==2 && p=1 { sub( /^[[:space:]]*"/, (++i==1?"":" ")"\"", $1 ); print $1; p=0 } END { print "\n" }' data
    

    输出:

    "Ok, so what Akanksha is saying is that..." "this is also not correct because it will take some time." "So, what you have to do is, what is the closest to the answer?"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      • 2015-06-12
      • 2016-09-07
      • 2011-03-29
      • 2021-12-13
      相关资源
      最近更新 更多