Solved

Java / Linux and Regular Expressions

Posted on 2016-11-10
11
68 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 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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

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.

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 …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

932 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now