首页 文章

使用sed / awk在每次出现模式时将长行分成多行

提问于
浏览
0

我有一个单行文件(实际上是一个很长的行),如下所示:

{worda:wordB:[{“wordc”,“active”:true},} {:wordb:“words”,“wordt”“wordu”“wordv”,“active”:true}等等 .

在这一行中,一个常见的模式是'“active”:true' . 我试图根据这种模式将这一行分成多行 .

必需输出:{worda:wordB:[{“wordc”,“active”:true} {:wordb:“words”,“wordt”“wordu”“wordv”,“active”:true}

我尝试使用sed和awk,或者在处理后产生0 Kb文件或者与输入文件相同的文件 .

我有Windows环境并使用GNU sed / awk .

请帮忙 .

2 回答

  • 0

    请根据论坛规则为您的帖子使用代码标记,请尝试关注,如果这有助于您,请告诉我们 .

    awk '{sub("true},}","true}");sub(/\"active\":true}/,"&\n")} 1'  Input_file
    

    输出如下 .

    {worda:wordB:[{"wordc","active":true}
    {:wordb:"words","wordt""wordu""wordv","active":true}
    

    此处也添加说明相同 .

    awk '{
    sub("true},}","true}");       ##Substitute string true},} to true} with the use of sub utility of awk here.
    sub(/\"active\":true}/,"&\n") ##Substituting "active":true} here with same string mentioned by & and a new line in current line.
    }
    1                             ##Mentioning 1 here, so awk works on condition and action. So by mentioning 1 here I am making condition TRUE here and NOT mentioning
                                  ##any action here, so by default print of current line will happen.
    ' Input_file                  ##Mentioning the Input_file here.
    
  • 0
    $ sed 's/true}.*{/true}\n{/' file 
    {worda:wordB:[{"wordc","active":true}
    {:wordb:"words","wordt""wordu""wordv","active":true}
    

    简要说明,使用 sedtrue}.*{ 替换为 true}\n{ ,其中 .* 将被视为任何字符, \n 将是换行符

相关问题