barefly
barefly

Reputation: 378

Using sed to replace curly braces

I am parsing some json data and I am in need of removing both beginning and ending curly braces, {}. I am using sed to perform this operation, but from what I can tell curly braces perform special functionality in sed and escaping them doesn't seem to be working. Here are a couple examples of what I have tried. I really could use a working regular expression. Thanks in advance!

First thing I tried that doesn't work.

sed 's/\{\}//g'

Some redone code from an answer I found here.

sed 's/\(\{\|\}\)//g'

Upvotes: 3

Views: 13326

Answers (1)

hek2mgl
hek2mgl

Reputation: 157967

I would use tr for that:

tr -d '{}' < file.json

With sed it should be:

sed 's/[{}]//g' file.json

[{}] is a character class that means { or }.

If you want to change the file in place pass the -i option to sed or use the sponge tool from moreutils. I like it because it is generic, meaning it will work with any command regardless if it supports in place editing or not:

sed 's/[{}]//g' file.json | sponge file.json

Upvotes: 10

Related Questions