Reputation: 153
I have a file which has content like this
Fri Sep 19 19:16:39 UTC 2014
Test launched. Check every minute for an update
minute 9:
ec2-54-84-5-158.compute-1.amazonaws.com: Requests per second: 926.13 [#/sec] (mean)
----------------------
minute 10:
ec2-54-84-5-158.compute-1.amazonaws.com: Requests per second: 919.23 [#/sec] (mean)
----------------------
minute 11:
ec2-54-209-65-210.compute-1.amazonaws.com: Requests per second: 547.62 [#/sec] (mean)
ec2-54-84-5-158.compute-1.amazonaws.com: Requests per second: 875.14 [#/sec] (mean)
----------------------
minute 12:
ec2-54-209-65-210.compute-1.amazonaws.com: Requests per second: 862.83 [#/sec] (mean)
ec2-54-84-5-158.compute-1.amazonaws.com: Requests per second: 876.78 [#/sec] (mean)
----------------------
minute 13:
ec2-54-209-65-210.compute-1.amazonaws.com: Requests per second: 857.37 [#/sec] (mean)
ec2-54-84-5-158.compute-1.amazonaws.com: Requests per second: 880.64 [#/sec] (mean)
----------------------
It basically keeps updating for every minute. So i want to get the last data from the last block between those two "------------"lines. the ultimate aim is to sum the requests per second for that block. Could you please guide me on the best logic to apply?
Upvotes: 0
Views: 173
Reputation: 4148
This code will identify the requests per second (field #5) and will identify the difference between the requests per second on the current line and the last (most recent) line that contained the number of requests per second.
This code assumes that the number of requests per second always appears on the line immediately after the line that starts with the world "minute" (without quotes).
try{
BufferedReader br = new BufferedReader(new FileReader("C:\\TEMP\\MINUTE.txt"));
String line;
String[] lineFields;
int i;
int thisLine_minute;
int lastLine_minute;
Double thisLine_reqpersec;
Double lastLine_reqpersec;
lastLine_minute = 0;
thisLine_reqpersec= 0.0;
lastLine_reqpersec= 0.0;
while ( (line = br.readLine()) != null) {
lineFields = line.split("\\s+");
System.out.printf("LINE READ: %s\n", line);
for (i=0; i < lineFields.length; i++) {
System.out.printf("FIELD: %d IS: %s\n", i, lineFields[i]);
}
if (lineFields[0].toUpperCase().equals("MINUTE")) {
thisLine_minute=1;
}
else {
thisLine_minute=0;
}
try {
if (lastLine_minute==1) {
lastLine_reqpersec = thisLine_reqpersec;
thisLine_reqpersec = new Double(lineFields[4]);
}
}
catch (Exception excepStrToInt) {
thisLine_reqpersec = 0.0;
}
System.out.printf("*** REQUESTS PER SECOND: %f\n",
thisLine_reqpersec);
System.out.printf("*** REQUESTS PER SECOND: DIFFERENCE: %f\n",
(thisLine_reqpersec- lastLine_reqpersec));
lastLine_minute=thisLine_minute;
}
br.close();
}
catch(Exception e){
}
Upvotes: 1
Reputation: 2027
So you're trying to sum the values within each block ended by a dashed line, right? Instead of trying to break the file apart, just read it, line-by-line, till you reach the end of the file, and react appropriately to whatever line you come to:
If you're looking to do something with all the per-second sums (like average them), then do whatever you need to do each time you finish reading a block. If all you care about is the last block, then just save off the last sum you computed each time, overwriting the last value; when you hit the last dashed line in the file, you'll have computed and saved the sum for the last block, and it'll be there for you to read and do what you need with it. But my intuition says you're going to want to use all the values, not just the last one...
Upvotes: 0
Reputation: 1704
String result="";
BufferedReader bf = new BufferedReader(new FileReader("your_file_path"));
int dashCount=0;
String finalResult= "";
while ((result = bf.readLine()) != null) {
if (result.length() != 0) {
if (result.charAt(0) == ’-’) {
dashCount=dashCount+1;
if(dashCount==2) break;
}
finalResult+=result;
}
}
Upvotes: 0
Reputation: 6816
You could probably do a split string
String yourBlocks[] = yourEntireFileInString.split("----------------------");
Then the block you want would be
yourBlocks[yourBlocks.length-2]
Further More what you want to do is
int numberRequest,numberRequest1;
String tempNumber;
for(String myString : yourBlocks) {
String [] splitedString = myString.split("Requests per second:");
tempNumber="";
for(int i = 0;i< splitedString[1].length ; i++) {
if(Charachter.isDigit()){
tempNumber+=splitedString[i];
}
if('#'==splitedString[i]){
break;
}
}
numberRequest = Integer.parseInt(tempNumber);
tempNumber="";
for(int i = 0;i< splitedString[3].length ; i++) {
if(Charachter.isDigit()){
tempNumber+=splitedString[i];
}
if('#'==splitedString[i]){
break;
}
}
numberRequest1 = Integer.parseInt(tempNumber);
// use numberRequest, numberRequest1 here
}
This might not be very efficient but will get you to the desired out put.
Upvotes: 0