0

I'm trying to split and sort this long weblog string:

"140.184.37.105 - - [08/Aug/2001:21:06:36 -0300] "GET /~csc226/outline.htm HTTP/1.0" 200 9748 "http://cs.stmarys.ca/~csc226/" "Mozilla/4.73 [en] (WinNT; U)""

My current code definitely doesn't work and I'm trying to figure out any ways to improve my code or use a different method of solving this problem. It obtains an array of many weblog entries, including the one above. I'm mainly trying to figure out how to sort the array to show the date as August 8, the ip address as 140.184.37.105, etc.

   void getString(int i)
   {
       return bin[i];
   }
   public void getIP(int i)
   {
      int IPlength = bin[i].length() - bin[i].indexOf("- -");
      String IP = bin[i].substring(0, bin[i].length()-IPlength);
   }
   void getDate(int i)
   {
      String Date = bin[i].substring(bin[i].indexOf('['), bin[i].indexOf(']'));
   }
   void getPage(int i)
   {
      String Page = bin[i].substring(bin[i].indexOf("GET"), bin[i].indexOf("1.0"));
   }

I'm sorry if this question is inconsiderate but I'm new and don't know how to approach or solve this problem.

The result should look something like this:

ip Address: 140.184.37.105

Date and Time enclosed in brackets: [08/Aug/2001:21:06:36 -0300]

Page Requested in quotes: "GET /~csc226/outline.htm HTTP/1.0"

HTTP Status Code returned to the client (200= successful): 200

4
  • If you clearly define what it is you're trying to do, there's a much better chance we can help you. There's even a chance you'll have a better idea of how to solve it. Commented Jun 10, 2014 at 5:59
  • So you want to sort them by date, or do you also want to modify the content as well (change the order)? Commented Jun 10, 2014 at 6:03
  • Why the solution you posted doesn't work? Commented Jun 10, 2014 at 6:04
  • i am only trying to sort the string and not modify any of the content in the string. i don't know how to store particular parts of the string into a variable. for example, i don't know how to store the date into the date variable. Commented Jun 10, 2014 at 6:06

2 Answers 2

1

If you absolutely want to write it yourself, I suggest you start brushing up on your regular expressions.

However, for this type of task, I would recommend using a log analysis tool like logstash.

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming that the format of string you gave will always remain same, Like this 140.184.37.105 - - [08/Aug/2001:21:06:36 -0300] "GET /~csc226/outline.htm HTTP/1.0" 200 9748 "http://cs.stmarys.ca/~csc226/" "Mozilla/4.73 [en] (WinNT; U)"

Its actually really simple, you just need to be familiar with methods of String class in java http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

For example:-

public class HelloWorld{

static String s="140.184.37.105 - - [08/Aug/2001:21:06:36 -0300] \"GET /~csc226/outline.htm HTTP/1.0\" 200 9748 \"http://cs.stmarys.ca/~csc226/\" \"Mozilla/4.73 [en] (WinNT; U)";


     public static void main(String []args){
        System.out.println(getIp());
        System.out.println(getTime());

     }


     public static String getIp(){

         String sArr[]= s.split("-");
         return sArr[0];


     }

     public static String getTime(){


         return s.substring(21,46);

     }

  //more functions here   

}

If your String format changes then you can go with regex, which will help you identify and find patterns in your String. Here is a good tutorial on regex in java http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

2 Comments

but what if the format doesn't stay the same? The getTime method will definitely have to search for the index of the beginning of the date. And is there any other way to do it other than regex?
Like I mentioned in my answer, If your String format changes you can use regex. It depends on your String. For example if you want to search date string and you know date will always begin and end within square braces [] and there wont be random braces before date , you can use something like s.substring(s.indexOf("["),s.indexOf("]")) and avoid regex. But if your String is really random and there maybe square braces even before date sub-string, then this might not work and you will have to opt for REGEX , Defining a pattern for date String and searching it in the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.