• Post Reply Bookmark Topic Watch Topic
  • New Topic

parsing logs using Regular expression.

 
Rishi Aggarwal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am new to java coding. so need suggestion on reading the log file using java code.

1) I wish to parse a file, and find a value for variable e.g. [StorageVersion].
2) After finding the value, it will be pushed to database.


Following is the code that i have written till now.





*************************
Sample Log File

{
//NAME
[1.2.2917] := s['3.4.454545'];

//Parameter
[1.2.2947] := s['1.2.56778'];

//Enabled
[1.2.3910] := B['22.43434.12232'];

//MAXCLOUMNNAME
[1.2.4217] := N['1.7.899982'];

//StorageVersion
[1.2.2917] := K['706146.0.22106932'];
}

************************

Help required :

1) I don't know what regular expression can i use.
2) I want a line to check that preceding line should have //StorageVersion .
3) n then i will check any string between := K[' and '];. From above log file "'706146.0.22106932'" is the value that i am looking for.

Thanks
Rishi
 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think you need a regex?
Why not just read the file in line by line until you find a line with //StorageVersion. Then read in the next line and use the String classes substring and lastIndexOf methods to extract the bit you want.
 
Arun Peter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Tony told, you need not use regex everywhere.
Also, K['+ is going to throw an error, as it will look for matching ]. You can checkout the modified pattern string below. For the third point, you will have to parse the string.


See this:


 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks very similar to the topic you started at http://www.coderanch.com/t/643058/java/java/logs-parsing. Is there a difference?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!