Results 1 to 11 of 11

Thread: Line parsing

  1. #1
    Unnel is offline Member
    Join Date
    Nov 2010
    Location
    Johannesburg
    Posts
    23
    Rep Power
    0

    Default Line parsing

    Hi,

    I've been experiencing difficulties to extract particular data from the lines in the text file that I'm reading. Because all the data in a line are not relevant for me I've been trying to extract the ones that are important. If you look at the example that I pasted below, you'll see that data are delimited by a space. I've been trying to read the lines and writing the elements of them that are relevant in a new file using in my parser the space command as a delimiter but unfortunately I'm not getting what I want.

    An example of what I mean with the line below is to write a loop that will extract for each line the Unix date (in the line below 122295697.113), the usage (5757) and the address (http:////skins.gmodules.com....), these are the only fields I need to write in a new file but the program that I wrote won't read them..Any hint?

    Thanks in advance,

    Below is the line (FYI it's a line taken out of a Squid Proxy Log file):

    1222952697.113 1321 146.141.28.175 TCP_MISS/200 5757 GET http://skins.gmodules.com/ig/skin_xml_to_css? STUDENTS\300160 DIRECT/209.85.129.99 text/css

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    see the split method of String.

  3. #3
    Unnel is offline Member
    Join Date
    Nov 2010
    Location
    Johannesburg
    Posts
    23
    Rep Power
    0

    Default Thanks

    I'll look at it..Thank you!!!:)

  4. #4
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Java Code:
    public class ParseStringDemo {
        public static void main(String[] args){
            String s = "1222952697.113 1321 146.141.28.175 TCP_MISS/200 5757 ";
            s = s + "GET http://skins.gmodules.com/ig/skin_xml_to_css? STUDENTS\300160 DIRECT/209.85.129.99 text/css ";
            String[] sp = s.split("\\s+");
            System.out.println( sp[0] );
            for(int i=1 ; i< sp.length;i++){
                String lstr = sp[i].toLowerCase();
                if( lstr.equals("get") || lstr.equals("post")  ){
                    System.out.println( sp[i-1] );
                    System.out.println( sp[i+1] );
                    break;
                }
            }
        }
    }

  5. #5
    sabhandari is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    This could also had been achieved by using StringTokenizer class

    My Java Blog

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by sabhandari View Post
    This could also had been achieved by using StringTokenizer class

    My Java Blog
    Not that you want to. StringTokenizer has some serious problems (especially with character sets other than ascii) and has been all but deprecated.

    And, if that recommendation is indicative of your blogging ....

  7. #7
    Unnel is offline Member
    Join Date
    Nov 2010
    Location
    Johannesburg
    Posts
    23
    Rep Power
    0

    Default

    Thanks for the tips guys,

    @JavaHater, I appreciate what u did but giving me the actual code is not very helpful since the line I gave as an example is just one in a couple of thousand others located in the same text file...No offence but I wish to do it myself so I'd rather have u giving me tips or 'How Tos' instead :)..Thanks

  8. #8
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by Unnel View Post
    Thanks for the tips guys,

    @JavaHater, I appreciate what u did but giving me the actual code is not very helpful since the line I gave as an example is just one in a couple of thousand others located in the same text file...No offence but I wish to do it myself so I'd rather have u giving me tips or 'How Tos' instead :)..Thanks
    The fact that I posted an example code IS giving you the tips you want. If you have taken the time to run it, understand what it does step by step, you will be able to implement it in your own code. That way, you are indeed doing it yourself. First of all, do you understand the code (without any comments? ) If you don't understand some of the methods and stuff, did you read up the docs? Now, all these you should do yourself, right?

  9. #9
    Unnel is offline Member
    Join Date
    Nov 2010
    Location
    Johannesburg
    Posts
    23
    Rep Power
    0

    Default

    Hi,

    Yes I understand what the code does, it extracts exactly the data (the 3 pieces of data) I'm looking for.Like I said in my previous reply, this works only for this particular line. The text file that I'm reading has got thousands of them and unfortunately the space used in between data is not uniform for every lines. What I mean is for some lines data can be separated by 1 or 2 spaces or even more..However I understood what you did, I'm just now trying to find a way to deal with the non-uniformity of the spaces used in the lines....:)

  10. #10
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by Unnel View Post
    Hi,

    Yes I understand what the code does, it extracts exactly the data (the 3 pieces of data) I'm looking for.Like I said in my previous reply, this works only for this particular line. The text file that I'm reading has got thousands of them and unfortunately the space used in between data is not uniform for every lines. What I mean is for some lines data can be separated by 1 or 2 spaces or even more..However I understood what you did, I'm just now trying to find a way to deal with the non-uniformity of the spaces used in the lines....:)
    then you should post a few more samples that illustrate those non-uniformity. you have only 1 sample line, and no where did you mention about non uniformity in your first post.

  11. #11
    Unnel is offline Member
    Join Date
    Nov 2010
    Location
    Johannesburg
    Posts
    23
    Rep Power
    0

    Default

    Hey,

    I managed to get it to work for every line of the file...The thing I had to do was just to change the String u used in your code (String s) and instead read every line as a String.

    Also, the delimiter you gave me actually took care of those non-uniform spaces (\\s+)..I also had to use loops to loop through each line and to read every line of the file.

    Thanks a lot for your help...:)

Similar Threads

  1. Replies: 8
    Last Post: 12-28-2010, 03:24 PM
  2. parsing command line arguments in java
    By farahm in forum Advanced Java
    Replies: 7
    Last Post: 11-21-2010, 04:36 PM
  3. Replies: 2
    Last Post: 07-02-2010, 03:20 PM
  4. Replies: 4
    Last Post: 09-10-2009, 11:18 AM
  5. Parsing a line
    By reis3k in forum New To Java
    Replies: 2
    Last Post: 08-18-2009, 05:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •