Okay, so I've got over a hundred JSON files with predictable bad formatting in several places per file.
Instead of using [ ] to indicate an array, they use { } instead.
For example:
"grid": { "C1", "D1", "E1", "C2", "D2", "E2", "F2", "B3", "C3", "D3", "E3", "F3", "B4", "C4", "D4", "E4", "F4", "C5", "D5", "E5", "F5", "C6", "D6", "E6" }, Each file has multiple arrays in it with this problem, each with a different key.
I came up with this to fix the above example, but it isn't very universal:
sed 's/^\t\t"grid": {/^\t\t"grid: [/; s/"E6" },$/"E6" ],/' myfile.json I also tried writing a more complicated awk script, something along these lines:
awk -i '/grid/ { gsub("{",{["); gsub("}","]") print $0 }' myfile.json But it replaced the contents of myfile.json to be only the row that contained the string "grid".
Is there a reliable one-liner to fix this issue?
{ print }action is implicit. Note thatgsubdoesn't print anything.-ioption for "in place"; rather, there is-i <include-file>. You can use-i inplaceto load the inplace extension. The semantics is still the same; the Awk produces output and that output replaces the file. If nothing is printed, the file will be empty.awk -i '{ ....code ...}1' fileThe added1will get the other lines to print. Also agree about using-i, better to get in the habit ofawk 'code' file >file.fix && mv file.fix file. There is no system economy in usinginplaceoptions, there is alway a temp file created. Good luck