I've been working on a small program, and am trying to find the best/simplest way to parse a text file. I originally started this project in C, but moved to Perl/Tk as it's much smaller and swifter for what I'm trying to do.
However, what I need to do is parse a text file. The file has a record for every second of the hour, and no delimiters.
The string is:
The format is as follows:
XXX = JulianDay; XX = Year; XXXXXX = Time; +XXXXXXX = value#1; -XXXXXXX = value#2; +XXXXXXX = value#3; -XXXXXXX = value#4; XXXXX.XX = Value#5; XXXXX.XX = value#6
(the + and - change depending on the value being recorded, but the format is still the same thruout the file)
Now, since there are no delimiters (spaces/commas/colons...) I'm stumped as to how I should go about reading this line, and splitting it up in its proper format for output.
Before it's asked, no I cannot change the text file or the program that outputs the text file.
Basically, what I'm trying to do is have the user input a time: XXXXXX and allow the program to parse thru the file to find that specific time and capture the entire line.
Then it needs to take that same line (or string) and format it for proper output to STDOUT or a file. (whichever)
I cannot seem to find anything that remotely resembles a clear-cut easy way to search/find specific values in a text file in C or in Perl.
So I tried using the system
function to call grep
which is an amasingly accurate search tool. Matter of fact, grep
does exactly what I'm trying to do in my program, which is why I'm trying to use it.
I initially tried:
system ("grep $TimeStr $FileA >>$FileOut");
which works, to print the exact string into a file, but I'm wondering if there is a way to have that same line of code print it to a $var instead of an output file.
system ("grep $TimeStr $FileA $var");
but that doesn't seem to work.
I also know that:
$var = substr ($input_str, 0, 3);
would work for part of what I'm trying to do, but I need to first get the grep
call to throw the line into a $var instead of a file.
Any ideas/thoughts are welcomed.
Also, I'm trying to limit the amount of external libs to use, to keep this as simple and compact as possible.