Solved

Java / Linux and Regular Expressions

Posted on 2016-11-10
11
101 Views
Last Modified: 2016-11-11
From within Java, I want to search an external text file called log.txt, starting at the bottom of the file, and read backwards, reading a word at a time, until I find a string sequence that starts with any a space, then any 5 Capital Letters, followed by a underscore, followed by the word "Forecast"

So, starting from the bottom of the file and working up, find the first occurrence of something like this:  " RAWV2_Forecast"

Ideally I would get the 5 letter sequence returned to the Java program
0
Comment
Question by:MarkLoveExEx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 41882120
Hi!

Look at these examples how to code regular expressions in Java.
http://www.java2s.com/Code/Java/Regular-Expressions/CatalogRegular-Expressions.htm

This example here could be a starting point to follow and code your problem
http://www.java2s.com/Code/Java/Regular-Expressions/Printallthestringsthatmatchagivenpatternfromafile.htm
Where you have the pattern something like this
Pattern patt = Pattern.compile("[A-Za-z]RAWV2_Forecast[A-Za-z]+");

Open in new window


Regards,
    Tomas Helgi
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41882261
How big is the file?

... then any 5 Capital Letters
In point of fact, one of the characters in your example is a number - which is it?
1
 
LVL 25

Assisted Solution

by:Tomas Helgi Johannsson
Tomas Helgi Johannsson earned 250 total points
ID: 41882314
Hi!

This pattern should give you all the  5 Capital Letters, followed by a underscore, followed by the word "Forecast"  

Pattern patt = Pattern.compile("[A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9]_Forecast[A-Za-z0-9]+");

Open in new window


Regards,
    Tomas Helgi
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 41882360
Read it all into one String, unless the file is too big to do so. The pattern will then be (to match the example rather than the description)

"\\s+[A-Z0-9]{5}_Forecast.*?$"

Open in new window


That will encourage the engine to search from the end backwards
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 41882402
Hi

Processing a file line by line is faster than loading the whole file into memory (a string)
especially with large files. Then this  approach as I mentioned in earlier comment
is better.

Regards,
    Tomas Helgi
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41882454
Processing a file line by line is faster than loading the whole file into memory
That's not normally the case and leads me to think that you probably don't know how file lines are processed in Java - actually not so trivial a task
0
 
LVL 25

Expert Comment

by:Tomas Helgi Johannsson
ID: 41882562
HI!

Working with files that are 10+ or even 100+ GB in size will lead to OutOfMemoryException where
the approach is to load the whole file into a String.

http://www.baeldung.com/java-read-lines-large-file

Regards,
     Tomas Helgi
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41882695
Well obviously if the file is huge - yes. That's why i asked how large it was
0
 

Author Comment

by:MarkLoveExEx
ID: 41883288
Thank you all for your input.  CEHJ, the file is not very big.  It is actually a log file that will grow over time, but it gets deleted every day.  I am not on the system at the moment, so I can't check its size right now.
0
 

Author Closing Comment

by:MarkLoveExEx
ID: 41883289
Thank you.  CEHJ...you're right, I do have a number in there with those letters.  I must be tired.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41883701
Well obviously if the file is huge - yes

. . .  and of course if it ever is huge, you can use nio's FileChannel and MappedByteBuffer classes to chop it up and process it in very large chunks.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question