Solved

Java / Linux and Regular Expressions

Posted on 2016-11-10
11
117 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

631 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