Reputation: 580
I am using trilead ssh2 to make ssh connection and parse the log files for every 5 minutes before.
I am using this syntax:-
grep '29/Jan/2018:[0-0][6-6]:[1-2][6-1]' /root/nohup.out>/tmp/nohup.txt
Basicaly I am building an android app which will parse log file for every 5 minutes interval and store it into /tmp/nohup.txt and will download and parse it to find the exceptions and show user those exception notification etc.
String parsingCommand="grep"+" ' "+day2+"/"+month2+"/"+year2+":"+"["+hour2/10+"-"+hour1/10+"]"+"["+hour2%10+"-"+hour1%10+"]"+":"+"["+minute2/10+"-"+minute1/10+"]"+"["+minute2%10+"-"+minute1%10+"]"+" ' "+"/root/nohup.out"+">"+"/tmp/nohup.txt";
Here minute1 = current minute fetched from server & minute2= minutes reduced by 5 minutes
grep '29/Jan/2018:[0-0][6-6]:[1-2][6-1]' /root/nohup.out>/tmp/nohup.txt
Here in this case minutes interval is 16-21.
I think i am not using proper regular expression .Because
grep '29/Jan/2018:[0-0][6-6]:[1-2][1-6]' /root/nohup.out>/tmp/nohup.txt
Its working.
Any help would be appreciated. Although so many answer are already here in stackoverflow:-
https://superuser.com/questions/439688/how-to-grep-a-log-file-within-a-specific-time-period
Upvotes: 1
Views: 1381
Reputation: 241
for the below input:
2018-01-29 08:00:30,393
2018-01-29 08:02:00,003
2018-01-29 08:03:00,210
2018-01-29 08:01:00,401
2018-01-29 08:01:00,401
2018-01-29 08:05:00,401
2018-01-29 08:16:00,002
2018-01-29 08:17:00,002
2018-01-29 08:18:00,002
2018-01-29 08:19:00,002
2018-01-29 08:20:00,002
2018-01-29 08:21:00,002
if you try running this regex:
2018-01-29 08:(0[0-4]|1[6-9]|2[0-1])
you will see a perfect match for 5 min. You will have to use the or operator for multi pattern matching.The way you re building the regex , you will have to do a lot of calculations. To save so much effort the solution provided by Daniel is proper as per your need.
Upvotes: 1
Reputation: 1594
I would use non capturing groups and "or":
egrep '29/Jan/2018:(?:(?:06:16)|(?:06:17)|(?:06:18)|(?:06:19)|(?:06:20)|(?:06:21))' /root/nohup.out>/tmp/nohup.out
Your current solution would also get entries from 06:11 and on the other hand miss entries from 06:20.
To include the date in the groups would even be better. Otherwise you could get problems at midnight:
egrep '(?:28/Jan/2018:23:59)|(?:29/Jan/2018:00:00)|(?:29/Jan/2018:00:01)|(?:29/Jan/2018:00:02)|(?:29/Jan/2018:00:03)' ...
You can accomplish that by using a StringBuilder:
public String getGrepCommand(final Date start) {
Calendar cal = Calendar.getInstance();
StringBuilder bld = new StringBuilder();
cal.setTime(start);
for (int i = 0; i < 5; ++i) {
bld.append("|(?:");
bld.append(String.format("%1$td/%1$tb/%1%tY:%1$tH:%1$tM", cal.getTime()));
bld.append(")");
cal.add(Calendar.MINUTE, 1);
}
if (bld.length() > 0) { // should be ;)
bld.delete(1, 1);
}
return bld.toString();
}
Upvotes: 2