MarkLoveExEx
asked on
Java / Linux and Regular Expressions
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
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
How big is the file?
... then any 5 Capital LettersIn point of fact, one of the characters in your example is a number - which is it?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
Processing a file line by line is faster than loading the whole file into memoryThat'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
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
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
Well obviously if the file is huge - yes. That's why i asked how large it was
ASKER
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.
ASKER
Thank you. CEHJ...you're right, I do have a number in there with those letters. I must be tired.
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.
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
Open in new window
Regards,
Tomas Helgi