Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java / Linux and Regular Expressions

Posted on 2016-11-10
11
Medium Priority
?
146 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
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 26

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 26

Assisted Solution

by:Tomas Helgi Johannsson
Tomas Helgi Johannsson earned 1000 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 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 26

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 26

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 17

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

927 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