我有一些json文件,其中有一些逗号出现在大括号
之前 "good": "line,"
"something": "blah",
}
然而,情况并非如此,
"also_good": "line",
"other": "blah2"
}
我在创建一个awk命令时遇到问题,该命令只会在新行上的大括号之前删除逗号。
答案 0 :(得分:2)
这awk
可能会:
awk '/^[ \t]*}/ {sub(/,$/,"",s)} NR>1 {print s} {s=$0} END {print s}' file
"good": "line,"
"something": "blah"
}
如果下一行以,
}
答案 1 :(得分:1)
使用GNU awk表示多字符RS和gensub()以及字符类缩写(例如\s
):
awk -v RS='^$' '{$0=gensub(/,(\s*\n\s*})/,"\\1","g")}1' file
e.g。当在此输入文件上运行时(注意保留第二个块之间的空格和}}:
$ cat file
"good": "line,"
"something": "blah",
}
"even_gooder": "line,"
"something": "note the spaces",
}
"also_good": "line",
"other": "blah2"
}
$ awk -v RS='^$' '{$0=gensub(/,(\s*\n\s*})/,"\\1","g")}1' file
"good": "line,"
"something": "blah"
}
"even_gooder": "line,"
"something": "note the spaces"
}
"also_good": "line",
"other": "blah2"
}