Text file analyser
Text file analyser
(OP)
I have an ongoing need for a program that will read in a text file and deconstruct it so that the important numerical results can be extracted and dumped into a spreadhseet.
For example
Some header stuff
Run 17
Number of widgets 16
Volume 8.3, 4.7
lots of stuff of variable length that I don't need that may include numbers
Run 18
Number of widgets 19
Volume 8.7, 5.6
followed by lots of other stuff I don't need that may include numbers
Would produce the output
17
16
8.3
4.7
18
19
8.7
5.6
only. The file to drive this would just contain "Run" and 4, ie give me the next 4 numbers after you see the phrase Run.
Obviously I can sort of do this by pulling it into excel, but it tends to be fragile and need a lot of customising.
Is there a general purpose solution to this - I'm guessing a database? I should point out that the files are very variable in length and the order in which things occure, but each segment that I am interested in is of a regular structure.
For example
Some header stuff
Run 17
Number of widgets 16
Volume 8.3, 4.7
lots of stuff of variable length that I don't need that may include numbers
Run 18
Number of widgets 19
Volume 8.7, 5.6
followed by lots of other stuff I don't need that may include numbers
Would produce the output
17
16
8.3
4.7
18
19
8.7
5.6
only. The file to drive this would just contain "Run" and 4, ie give me the next 4 numbers after you see the phrase Run.
Obviously I can sort of do this by pulling it into excel, but it tends to be fragile and need a lot of customising.
Is there a general purpose solution to this - I'm guessing a database? I should point out that the files are very variable in length and the order in which things occure, but each segment that I am interested in is of a regular structure.
Cheers
Greg Locock
SIG:Please see FAQ731-376: Eng-Tips.com Forum Policies for tips on how to make the best use of Eng-Tips.






RE: Text file analyser
http://www.guysoftware.com/parserat.htm
RE: Text file analyser
- Steve
RE: Text file analyser
- Steve
RE: Text file analyser
katmar- good try. see also Depeche View. I'll have a play tomorrow.
SG - Well, no I haven't got a C compiler (a nasty language) but that is not a restriction I placed, so go for it. I do have a C to Fortran converter (grins).
For what its worth I've got a matlab prototype, but it is long on horrible and short on flexibility
Cheers
Greg Locock
SIG:Please see FAQ731-376: Eng-Tips.com Forum Policies for tips on how to make the best use of Eng-Tips.
RE: Text file analyser
On my point of view You should use VisualBasic inside an Excel Spreadsheet.
Cheers
Onda
RE: Text file analyser
http://sbutton.tripod.com/deadpig/
TTFN
FAQ731-376: Eng-Tips.com Forum Policies
RE: Text file analyser
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
RE: Text file analyser
/******************************/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
int main(int argc, char *argv[])
{
char *fname=argv[1];
char *key=argv[2];
int n=atoi(argv[3]);
char line[1024], *p;
int searching, count;
float value;
FILE *fp=fopen(fname,"r");
if(argc<4)
{
printf("Usage %s file key count\n", argv[0]);
return 1;
}
searching=1;
while(fgets(line, 1024, fp))
{
p=line;
if(searching && !(p=strstr(p,key))) continue;
searching=0;
strtok(p," ,"); /* Assume there is always a throwaway leading token */
while(p=strtok(NULL, " ,"))
{
if(sscanf(p, "%f", &value)==1)
{
printf("%g\n", value);
if(++count==n)
{
searching=1;
count=0;
break;
}
}
}
}
fclose(fp);
return 0;
}
/******************************/
- Steve
RE: Text file analyser
I'll run it through the scruncherator/compiler tomorrow and see what it does. He says with his heart in his mouth.
Cheers
Greg Locock
SIG:Please see FAQ731-376: Eng-Tips.com Forum Policies for tips on how to make the best use of Eng-Tips.
RE: Text file analyser
If you have Matlab for Windoze, then you have LCC. A really rubbish C compiler, but it does actually work.
Give me my dues though, there is one comment in that program
- Steve
RE: Text file analyser
Live Long and Prosper !
RE: Text file analyser
Live Long and Prosper !
RE: Text file analyser
Mike Halloran
Pembroke Pines, FL, USA
RE: Text file analyser
Live Long and Prosper !
RE: Text file analyser
Cheers,
Joerd
Please see FAQ731-376: Eng-Tips.com Forum Policies for tips on how to make the best use of Eng-Tips.
RE: Text file analyser
Ken
RE: Text file analyser
Actually I screwed up, the output in the example I gave should only have been
17
16
8.3
4.7
Cheers
Greg Locock
SIG:Please see FAQ731-376: Eng-Tips.com Forum Policies for tips on how to make the best use of Eng-Tips.
RE: Text file analyser
Open the file.
Read each line (1 at a time) into a long character string.
Use the INDEX() function to search for "RUN" until it is found.
After finding "RUN" search the remainder of that line and each additional line for numeric data (ie. just seach form digits and decimal pts or whatever ). When found, Do a READ on the numeric characters to get the data into a numeric data type.
Continue doing this with each line until the desired # of data items are found or the file comes to an end.
Dan
RE: Text file analyser
Here'a VBA code to read and analyze the text files. Executable sub: ReadFile.
CODE
Sub ReadFile()
Dim TextLine As String
Dim strWords(1 To 50) As String
Dim Nwords%, Nlines%, iLine%, iword%
Dim OutputNumber(1 To 1000)
Open Application.GetOpenFilename() For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, TextLine ' Read line into variable.
Nwords = SeparateWords(TextLine, strWords())
iLine = iLine + 1
For iword = 1 To Nwords
If IsNumeric(strWords(iword)) Then
OutputNumber(iLine) = Val(strWords(iword))
Exit For
Else
OutputNumber(iLine) = ""
End If
Next i
Range("A1")(iLine) = OutputNumber(iLine)
Loop
Close #1 ' Close file.
End Sub
Function SeparateWords(RightText As String, strWords() As String) As Integer
Dim iwords As Integer
'''
iwords = 0
'''
Do Until RightText = ""
iwords = iwords + 1
strWords(iwords) = NextWord(RightText)
Loop
SeparateWords = iwords
End Function
Private Function NextWord(ByRef tempText As String) As String
'''function returns string between the spaces and trims input string
Dim CharCounter As Long
'''
CharCounter = 1
tempText = LTrim(tempText)
Do While Mid(tempText, CharCounter, 1) <> " " And CharCounter <= Len(tempText)
CharCounter = CharCounter + 1
Loop
NextWord = Mid(tempText, 1, CharCounter - 1)
tempText = Mid(tempText, CharCounter)
'''
End Function
RE: Text file analyser
see: thread227-233088: New Parser Generator someone has written yet another compiler compiler.
TTFN
FAQ731-376: Eng-Tips.com Forum Policies
RE: Text file analyser
Cheers
Greg Locock
SIG:Please see FAQ731-376: Eng-Tips.com Forum Policies for tips on how to make the best use of Eng-Tips.
RE: Text file analyser
One day though, I'll have enough spare time to sxplore yacc.
- Steve
RE: Text file analyser
I'll have to look that home for the download.
TTFN
FAQ731-376: Eng-Tips.com Forum Policies
RE: Text file analyser
- Steve
RE: Text file analyser
TTFN
FAQ731-376: Eng-Tips.com Forum Policies