查找具有奇数个大括号的行
我有一个包含 {
和 }
的文档,但我发现有错误。我使用 grep -c { *
和 grep -c } *
发现 {
和 }
的数量不相等。我想找到那些可能存在错误的行,这样我就可以手动检查它们。
- 右大括号应始终出现在同一行,因此我需要查找带有
{
且没有右}
的行或带有} 不带左大括号。
- 有些行有很多大括号,例如
{ } { } { }
。 - 永远不应该有嵌套的大括号,例如“{ { { } } }”。
如何搜索大括号数量不正确的行?
I have a document containing {
and }
throughout, but I found there are mistakes. I used grep -c { *
and grep -c } *
to find that the number of {
and }
is not equal. I want to find those lines with possible errors, so I can manually check them.
- The closing braces should always appear on the same line, so I need to find lines with
{
that don't have a closing}
or lines with}
without the opening brace. - Some lines have many braces, e.g.
{ } { } { }
. - There never should be nested braces, e.g. `{ { { } } }'.
How can I search for lines that do not have the correct number of braces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以做这样的事情(这将打印行和行号) -
测试:
You can do something like this (This will print the line and the line number) -
Test:
这可能对你有用:
This might work for you:
试试这个代码。它将打印大括号计数不匹配的那些行
Try this code. It will print those lines for which the brace count dosen't match
我不在我的计算机上尝试,但我认为这可能会起作用
如果我写得正确,这应该匹配(所以不打印,因为
-v
)只有整个行该行由对组成,重复零次或多次。
I'm not at my computer to try, but I think this might work
If I've written it correctly, this should match (so not print, because of the
-v
) only lines where the entire line consists of pairs such thatRepeated zero or more times.