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" *
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With