简体   繁体   中英

How to check if “{” begins in separate line in java file using bash script

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.

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.

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM