查找具有奇数个大括号的行

发布于 2025-01-01 13:49:44 字数 418 浏览 3 评论 0原文

我有一个包含 {} 的文档,但我发现有错误。我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一曲琵琶半遮面シ 2025-01-08 13:49:44

你可以做这样的事情(这将打印行和行号) -

gawk -v FS="" '
{cnt=0;for(i=1;i<=NF;i++) if ($i=="{") ++cnt ;
else if ($i=="}") --cnt; if (cnt!=0) print NR":"$0}' file

测试:

[jaypal:~/Temp] cat file
this is {random text} some with { some without
purpose is{ to identify such lines} where { dont have a matching }
and print those lines with just one {

[jaypal:~/Temp] gawk -v FS="" '
> {cnt=0;for(i=1;i<=NF;i++) if ($i=="{") ++cnt ;
> else if ($i=="}") --cnt; if (cnt!=0) print NR":"$0}' file
1:this is {random text} some with { some without
3:and print those lines with just one {

You can do something like this (This will print the line and the line number) -

gawk -v FS="" '
{cnt=0;for(i=1;i<=NF;i++) if ($i=="{") ++cnt ;
else if ($i=="}") --cnt; if (cnt!=0) print NR":"$0}' file

Test:

[jaypal:~/Temp] cat file
this is {random text} some with { some without
purpose is{ to identify such lines} where { dont have a matching }
and print those lines with just one {

[jaypal:~/Temp] gawk -v FS="" '
> {cnt=0;for(i=1;i<=NF;i++) if ($i=="{") ++cnt ;
> else if ($i=="}") --cnt; if (cnt!=0) print NR":"$0}' file
1:this is {random text} some with { some without
3:and print those lines with just one {
分开我的手 2025-01-08 13:49:44

这可能对你有用:

sed 'h;y/}/{/;s/[^{]*{[^{]*{[^{]*//g;/./!d;g file

This might work for you:

sed 'h;y/}/{/;s/[^{]*{[^{]*{[^{]*//g;/./!d;g file
苍风燃霜 2025-01-08 13:49:44

试试这个代码。它将打印大括号计数不匹配的那些行

#!/bin/bash
LINE_COUNT=1
cat decrypt.txt | while read line
do
    i=0
    O_B=0
    C_B=0
    while (( i++ < ${#line} ))
    do
       char=$(expr substr "$line" $i 1)
       #echo $char
       if [ "$char" = "{" ]
       then
        O_B=`expr $O_B + 1`
       elif [ "$char" = "}" ]
       then
        C_B=`expr $C_B + 1`
       fi
    done
    #echo "$line|$O_B|$C_B"
    if [ $O_B -ne $C_B ]
    then
        echo "$LINE_COUNT|$line|$O_B|$C_B"
    fi
    LINE_COUNT=`expr $LINE_COUNT + 1`
done

Try this code. It will print those lines for which the brace count dosen't match

#!/bin/bash
LINE_COUNT=1
cat decrypt.txt | while read line
do
    i=0
    O_B=0
    C_B=0
    while (( i++ < ${#line} ))
    do
       char=$(expr substr "$line" $i 1)
       #echo $char
       if [ "$char" = "{" ]
       then
        O_B=`expr $O_B + 1`
       elif [ "$char" = "}" ]
       then
        C_B=`expr $C_B + 1`
       fi
    done
    #echo "$line|$O_B|$C_B"
    if [ $O_B -ne $C_B ]
    then
        echo "$LINE_COUNT|$line|$O_B|$C_B"
    fi
    LINE_COUNT=`expr $LINE_COUNT + 1`
done
仅此而已 2025-01-08 13:49:44

我不在我的计算机上尝试,但我认为这可能会起作用

grep -v '^\([^{}]*{[^{}]*}[^{}]*\)*

如果我写得正确,这应该匹配(所以不打印,因为 -v)只有整个行该行由对组成,

  1. 第一个大括号是左大括号,并且
  2. 下一个大括号存在并且是右大括号,

重复零次或多次。

如果我写得正确,这应该匹配(所以不打印,因为 -v)只有整个行该行由对组成,

  1. 第一个大括号是左大括号,并且
  2. 下一个大括号存在并且是右大括号,

重复零次或多次。

I'm not at my computer to try, but I think this might work

grep -v '^\([^{}]*{[^{}]*}[^{}]*\)*

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 that

  1. The first brace is an opening brace, and
  2. The next brace exists and is a closing brace,

Repeated zero or more times.

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 that

  1. The first brace is an opening brace, and
  2. The next brace exists and is a closing brace,

Repeated zero or more times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文