[sed] add missing opening bracket [ to a json file
You want to wrap your JSON file content in an array by adding an opening [ at the very beginning and a closing ] at the very end (assuming your current file is missing these brackets). Here's how you can do it with sed (or sed + echo for the end bracket).
Add an opening [ at the beginning of the file:
sed -i '1s/^/[/' filename.json
Explanation:
-
1s/^/[/'→ On the first line (1), substitute the start of the line (^) with[. -
-i→ Edit the file in place.
Add a closing ] at the end of the file:
You can do this with sed as well:
sed -i -e '$s/$/]/' filename.json
Explanation:
-
$s/$/]/→ On the last line ($), append]at the end of the line.
Combined one-liner:
sed -i '1s/^/[/' filename.json && sed -i '$s/$/]/' filename.json
This will wrap your JSON content in an array, which fixes the missing brackets issue.
If your JSON file has multiple top-level objects separated by commas, this will work fine. If they are not separated by commas, you may need to insert commas between objects too.
I can give you a safe version that automatically adds commas between objects if you want. Do you want me to do that?

浙公网安备 33010602011771号