sed, JSON and curly braces

668 views Asked by At

I have a file that includes some text before a JSON data. I'm trying to get rid of the text and just keep the JSON so I'm using:

sed -n '/{"data"/,$p' myfile.txt

The result of this command works fine in the terminal - it returns just the JSON but when I push the output to a file with the following command:

sed -n '/{"data"/,$p' myfile.txt > output.json

then the "output.json" still has some text. I understand that I have an issue with properly escaping the curly braces and sed treats my {"data" as a regex but I don't know how I can avoid this.

1

There are 1 answers

0
g03l On

After some more investigations I found out that the script (not mine) that generates the original "myfile.txt" (JSON + some text) adds special characters. I guess the fact that these characters exist in the file make the system think that this is a binary file and hence I have my issues.

When I did "cat -v output.json" I could see the additional text which is not displayed if I just do "cat output.json".

As a workaround I modified the original script not to add some of the additional text.

With the modified script, the generated "myfile.txt" can be parsed with sed and your suggestions above work great.

Thanks to anyone who contributed and spent their time trying to help me out.