bash 使用 sed 替换花括号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38513319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:55:44  来源:igfitidea点击:

Using sed to replace curly braces

bashsed

提问by barefly

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!

我正在解析一些 json 数据,我需要删除开头和结尾的大括号 {}。我正在使用 sed 来执行此操作,但据我所知,花括号在 sed 中执行特殊功能并且转义它们似乎不起作用。以下是我尝试过的几个例子。我真的可以使用一个有效的正则表达式。提前致谢!

First thing I tried that doesn't work.

我尝试的第一件事不起作用。

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

Some redone code from an answer I found here.

我在这里找到的答案中的一些重做代码。

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

回答by hek2mgl

I would use trfor that:

我会用tr

tr -d '{}' < file.json


With sedit should be:

有了sed它应该是:

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

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

[{}]是一个字符类,表示{}

If you want to change the file in place pass the -ioption to sedor use the spongetool 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:

如果你想改变的地方文件传递-i选项sed或使用sponge从工具moreutils。我喜欢它,因为它是通用的,这意味着它可以与任何命令一起使用,无论它是否支持就地编辑:

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