• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

parsing logs using Regular expression.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks very similar to the topic you started at https://coderanch.com/t/643058/java/java/logs-parsing. Is there a difference?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic