java - How to print out a string between certain dates from log file -
so i've got log file need search strings , print out lines fall in between date ranges.
ive got first part done im stuck on second. how print out string within date. want print strings between 2012-09-01 , 2012-09-15
log example:
2012-09-13 16:04:22 debug sid:34523 bid:1329 rid:65d33 'starting new session' 2012-09-13 16:04:30 debug sid:34523 bid:1329 rid:54f22 'authenticating user' 2012-09-13 16:05:30 debug sid:42111 bid:319 rid:65a23 'starting new session' 2012-09-13 16:04:50 error sid:34523 bid:1329 rid:54ff3 'missing authentication token' 2012-09-13 16:05:31 debug sid:42111 bid:319 rid:86472 'authenticating user' 2012-09-13 16:05:31 debug sid:42111 bid:319 rid:7a323 'deleting asset id 543234' 2012-09-13 16:05:32 warn sid:42111 bid:319 rid:7a323 'invalid asset id'
this code far:
import java.io.file; import java.io.filenotfoundexception; import java.util.arraylist; import java.util.date; import java.util.scanner; public class readlogs { public static void main(string args[]) throws filenotfoundexception{ string line, logstring =""; date startdate, enddate; arraylist<string> loglist = new arraylist<>(); scanner logscanner = new scanner(new file("c:\\users\\cmccarth\\desktop\\logs.txt")); while (logscanner.hasnextline()) { line = logscanner.nextline(); loglist.add(line); } (string z : loglist) { // system.out.println(z); } // function prints out lines containing specific string for( int = 0; <= loglist.size() - 1; i++) { logstring = loglist.get(i); if(logstring.contains("16:04:22")){ system.out.println("string contains" +logstring); } } } }
perform following steps :
1) create 2 date objects date range want search in ( these dates using simple date format) :
date lowerrange; date upperrange;
2) loop through filtered log list , okenize string log file , date tokens[0].
string[] tokens = logstring.split(" "); string datestr = tokens[0]; // convert date string using simple date format date date = new simpledateformat("yyyy-mm-dd", locale.english).parse(datestr);
3) can compare date 2 dates have see if falls in range.
if(date > lowerrange && date < upperrange) { // date wanted...process log file entry }
refer following post string date conversion : string-to-date
Comments
Post a Comment