0

I have to count how many occurrences of certain strings appear in a log4j file. I have a start date from which I want to start greping (maybe sed or awk?) in a Java class during a test.

Even if I don't know if it's better to capture the grep result from a Runtime.getRuntime().exec(...) command versus reading and parsing (via regex and string manipulation) the current log4j file, I don't know how to grep a file like this:

2016-07-29 15:13:08,725:  INFO [main] .....
2016-07-29 15:13:08,817:  INFO [main] ....
2016-07-29 15:13:08,901:  INFO [main] ...

but only after 2016-07-29 15:13:08,818 for example

Thanks in advance!

jonayreyes
  • 538
  • 1
  • 10
  • 27

1 Answers1

0

If you have an exact match from which you want divide the output, as per another question here you can use sed to only output the lines from and after that timestamp. Finding that exact match on which to break probably means progressively making your timestamp more lenient e.g. dropping the last character until you get a match/matches, then parsing those matches to decide which is the first after.

If you'd like to do that in bash, consider this question; specifically converting the dates to unix timestamps and then comparing the resultant values. The rest should be straightforward; equally in Java, extracting the date and comparing them should not be too challenging. Use the language that you're more familiar with unless you have the bandwidth for a learning experience.

Community
  • 1
  • 1
Danikov
  • 735
  • 3
  • 10
  • Thanks @Danikov but I just found I have a `mytoken` that I could use, so I can split my file in two after its last occurrence. After that, I can feed the output in my java program without messing with dates. Ultimately, there has to be some string processing there. I'll accept your answer though, for pointing to that excellent resource for `sed` vs `grep` – jonayreyes Aug 01 '16 at 10:57
  • 1
    Actually, I did exactly as shown here http://stackoverflow.com/a/5935779/1983647 but replacing `tail -r` with `tac` – jonayreyes Aug 01 '16 at 12:33