user3270760 user3270760 - 4 months ago 10
Java Question

Pull entire Java statement out of file using Bash

I have several directories which contain large Java files and I would like to pull out all the log messages. This includes log.error, .info, etc. In general, they look something like this:

logger.error("some message here");


The problem is that some of these messages include line breaks, and therefore grep is not picking up the full message:

logger.debug("operation [" + j + "] = whatever " + ids[j] +
" name: " + names[j] + " time: " + times[j]);


Is there a way that I can use regular expressions to get the entire Java statement, up to the semicolon?

Here is what I have so far:

grep -rn --include \*.java "\b\.error(\"\b" *

Answer

Try:

find . -iname '*.java' -exec awk '/logger/,/;/' *.java +

As an example, let's consider this test file:

$ cat file.java 
some(text);
logger.debug("operation [" + j + "] = whatever " + ids[j] + 
" name: " + names[j] + " time: " + times[j]);
other(text);
logger.error("some message here");
more(text); 

Let's extract its logger statements:

$ find . -iname '*.java' -exec awk '/logger/,/;/' {} +
logger.debug("operation [" + j + "] = whatever " + ids[j] + 
" name: " + names[j] + " time: " + times[j]);
logger.error("some message here");

This works by looking for lines that contain logger and printing every line from there to the first line that contains ;.

As Henry points out in the comments, regex algorithms like this are not foolproof. But, if you are using this just for visual inspection, this should be a good start.

If you also want to record the file name and line number:

$ find . -iname '*.java' -exec awk '/logger/,/;/{printf "%s:%s: %s\n",FILENAME,FNR,$0}' {} +
./file.java:2: logger.debug("operation [" + j + "] = whatever " + ids[j] + 
./file.java:3: " name: " + names[j] + " time: " + times[j]);
./file.java:5: logger.error("some message here");
Comments