如何使用bash脚本检查“{”是否在java文件的单独行中开始

时间:2021-09-08 01:47:52

I have a set of .java files in one directory. Now i want to check if the { (left brace) is beginning a new line or not.

我在一个目录中有一组.java文件。现在我想检查{(左括号)是否开始换行。

For example,

class Name{
  public static void main(String args[])
  {
    System.out.println("Hello");
  }
}

The script has to check how many { are beginning a new line and how many are not, and give a count.

该脚本必须检查有多少{正在开始新行,有多少不行,并计算。

2 个解决方案

#1


1  

Use grep -c '^\s*{' <filename> to get the number of opening braces "{" that are not preceded by anything else on their line. This would however also count lines where some text follows a brace. So you might want to use grep -c '^\s*{\s*$', which counts lines that only contain a brace (sourounded by arbitrary white-space).

使用grep -c'^ \ s * {' 来获取开头大括号“{”的数量,这些大括号“{”之前没有任何其他内容。然而,这也会计算一些文本跟随括号的行。因此,您可能希望使用grep -c'^ \ s * {\ s * $',它计算仅包含括号的行(由任意空格环绕)。

#2


0  

i want to check if the "{" (parenthesis) is beginning from separate line or not.

我想检查“{”(括号)是否从单独的行开始。

It's not clear exactly what you mean, but if you are simply wanting to know if the "{" char appears in the first position on a line, then "^" in a grep or egrep matches as "starts with". So grep ^{ *.java would find those lines. If that's not what you mean, please change your question text to be specific.

目前尚不清楚你的意思,但如果你只是想知道“{”字符是否出现在一行的第一个位置,那么grep或egrep中的“^”就会匹配为“以...开头”。所以grep ^ {* .java会找到那些行。如果这不是您的意思,请将您的问题文本更改为具体。

#1


1  

Use grep -c '^\s*{' <filename> to get the number of opening braces "{" that are not preceded by anything else on their line. This would however also count lines where some text follows a brace. So you might want to use grep -c '^\s*{\s*$', which counts lines that only contain a brace (sourounded by arbitrary white-space).

使用grep -c'^ \ s * {' 来获取开头大括号“{”的数量,这些大括号“{”之前没有任何其他内容。然而,这也会计算一些文本跟随括号的行。因此,您可能希望使用grep -c'^ \ s * {\ s * $',它计算仅包含括号的行(由任意空格环绕)。

#2


0  

i want to check if the "{" (parenthesis) is beginning from separate line or not.

我想检查“{”(括号)是否从单独的行开始。

It's not clear exactly what you mean, but if you are simply wanting to know if the "{" char appears in the first position on a line, then "^" in a grep or egrep matches as "starts with". So grep ^{ *.java would find those lines. If that's not what you mean, please change your question text to be specific.

目前尚不清楚你的意思,但如果你只是想知道“{”字符是否出现在一行的第一个位置,那么grep或egrep中的“^”就会匹配为“以...开头”。所以grep ^ {* .java会找到那些行。如果这不是您的意思,请将您的问题文本更改为具体。