Solved

Text File reading

Posted on 1997-08-31
8
208 Views
Last Modified: 2010-04-04
Hello,

Q. 1st - A want to read a text file (line by line is okay).
   2nd - I want to locate certain keywords.
   3rd - Once those keywords are located, I want to write          particular sections of that line with the keyword          to a TStringGrid.

File example:
TITLE     EXAMPLE INPUT FILE    2D   DATA
ERRORS           3.0       1.0       2.0       1.0
REDUN    

COORDINATE    4                      8669.           4674.  
COORDINATE   11 EN  Evernden          451.011        884.817
COORDINATE   12 EN  Ovens           22099.220       1416.713

DIRECTION     1    7      187 43 19.  
DIRECTION     2    1       30 21 49.        
DIRECTION     2    3      257 34 24.      
     
H DIS         1    2       538.103      
H DIS         1    5       552.968    
H DIS         1    7      1655.179  

Here I want to locate the keywords 'COO', 'DIR', and 'H DI'.
Once those keywords are located, I want the text at certain positions in that line with keyword.

ie. If the keyword is 'DIR', I would like the text in         position 12 to 15 => "   2"    
    and      17 to 20 => "   3"
             27 to 29 => "257"  etc    
    If the keyword is 'H DI', I would like the text in          position  12 to 15 => "   1"    
    and       17 to 20 => "   2"    
    and       27 to 34 => "538.103"    etc

    (whether it be blanks or what not)

12345678901234567890123456789012345678901234567890
0        1         2         3         4         5
          !    !    !    !   !  !    !
DIRECTION     2    3      257 34 24.      
                          !       !
H DIS         1    2       538.103
0
Comment
Question by:Ham
  • 5
  • 2
8 Comments
 

Expert Comment

by:Bueno
Comment Utility
Hi Ham,
I would like to help you, but you should have to be a little bit more specific... In your case the reasonable thing to do is to split the string into tokens and put them in the TStringGrid.

Unfortunatly you gathered this problem as if it is a fixed text, and I do not know why. Please give me some more information about your problem.

-- Again, the reasonable thing to do is to split the strings, usings predefined keywords (HDIS, COO, DIR... etc) and split the words as tokens using the ' ' space delimiter.
Waiting for your answer...
Amit
0
 

Author Comment

by:Ham
Comment Utility
Hello,
I thought my question was specific enough!
The example was there - it is possible that the formatting once sent wasnt the same as typed? This may of lead to some confusion.

What other information would you require in order to solve this problem?

However I will try and reword the question -

Q. 1st - A want to read a text file (line by line is okay).
   2nd - I want to locate certain keywords with in the text file.
   3rd - Once those keywords are located, I want to write                 particular sections of that line that has the keyword            to a TStringGrid (I want to disect the line not using          spaces, not commas or not tabs but using position).

File example:
TITLE     EXAMPLE INPUT FILE    2D   DATA
ERRORS           3.0       1.0       2.0       1.0
REDUN    

COORDINATE    4                      8669.           4674.  
COORDINATE   11 EN  Evernden          451.011        884.817
COORDINATE   12 EN  Ovens           22099.220       1416.713

DIRECTION     1    7      187 43 19.  
DIRECTION     2    1       30 21 49.        
DIRECTION     2    3      257 34 24.      
     
H DIS         1    2       538.103      
H DIS         1    5       552.968    
H DIS         1    7      1655.179  

In the above example I want to locate the keywords 'COO', 'DIR', and 'H DI'.
Once those keywords are located, I want to take sections of the remaining text on that line that contains the keyword.

ie. If the keyword is 'DIR', I would like the text from position     12 to 15 => "   2"    
    17 to 20 => "   3"
    27 to 29 => "257"  etc    
   
    If the keyword is 'H DI', I would like the text in position      12 to 15 => "   1"    
    17 to 20 => "   2"    
    27 to 34 => "538.103"    etc

    (whether the text is blanks, commas or what ever)
The numbers below (2 lines) are just for position reference:
12345678901234567890123456789012345678901234567890
0        1         2         3         4         5
The line below is a marker reference showing the positions wanted
          !    !    !    !   !  !    !
The line below is a line from the above example that contains the keyword and text that I want to extract not based on spaces, commas or tabs but wanting to use positions:
DIRECTION     2    3      257 34 24.      
The line below is a marker reference showing the positions wanted
                          !       !
The line below is a line from the above example that contains the keyword and text that I want to extract not based on spaces, commas or tabs but wanting to use positions:
H DIS         1    2       538.103

The alignment of the references (numbers and marker) if formatted properly will demostrate the area of text that I require.

Thankyou, Ham
0
 

Author Comment

by:Ham
Comment Utility
Hello,
I thought my question was specific enough!
The example was there - it is possible that the formatting once sent wasnt the same as typed? This may of lead to some confusion.

What other information would you require in order to solve this problem?

However I will try and reword the question -

Q. 1st - A want to read a text file (line by line is okay).
   2nd - I want to locate certain keywords with in the text file.
   3rd - Once those keywords are located, I want to write                 particular sections of that line that has the keyword            to a TStringGrid (I want to disect the line not using          spaces, not commas or not tabs but using position).

File example:
TITLE     EXAMPLE INPUT FILE    2D   DATA
ERRORS           3.0       1.0       2.0       1.0
REDUN    

COORDINATE    4                      8669.           4674.  
COORDINATE   11 EN  Evernden          451.011        884.817
COORDINATE   12 EN  Ovens           22099.220       1416.713

DIRECTION     1    7      187 43 19.  
DIRECTION     2    1       30 21 49.        
DIRECTION     2    3      257 34 24.      
     
H DIS         1    2       538.103      
H DIS         1    5       552.968    
H DIS         1    7      1655.179  

In the above example I want to locate the keywords 'COO', 'DIR', and 'H DI'.
Once those keywords are located, I want to take sections of the remaining text on that line that contains the keyword.

ie. If the keyword is 'DIR', I would like the text from position     12 to 15 => "   2"    
    17 to 20 => "   3"
    27 to 29 => "257"  etc    
   
    If the keyword is 'H DI', I would like the text in position      12 to 15 => "   1"    
    17 to 20 => "   2"    
    27 to 34 => "538.103"    etc

    (whether the text is blanks, commas or what ever)
The numbers below (2 lines) are just for position reference:
12345678901234567890123456789012345678901234567890
0        1         2         3         4         5
The line below is a marker reference showing the positions wanted
          !    !    !    !   !  !    !
The line below is a line from the above example that contains the keyword and text that I want to extract not based on spaces, commas or tabs but wanting to use positions:
DIRECTION     2    3      257 34 24.      
The line below is a marker reference showing the positions wanted
                          !       !
The line below is a line from the above example that contains the keyword and text that I want to extract not based on spaces, commas or tabs but wanting to use positions:
H DIS         1    2       538.103

The alignment of the references (numbers and marker) if formatted properly will demostrate the area of text that I require.

Thankyou, Ham
0
 

Author Comment

by:Ham
Comment Utility
You never replied
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Accepted Solution

by:
CFantin earned 20 total points
Comment Utility
Here is some code to extract the data from the file and to check for the keywords.

I was not to clear on what the stringlist should look like though.

procedure TForm1.Button1Click(Sender: TObject);
var F: TextFile;
    SL: StringList;
    FileLine, FormatLine: String;
begin
  SL := TStringList.Create;
  AssignFile(F, 'File Name goes here with full path');
  Reset(F);
  while not EOF(F) do
  begin
    ReadLn(F, FileLine);
    if Pos('DIR', FileLine) > 0 then  //If the Keyword is DIR
    begin
      SL.Add('DIR'+
            Copy(FileLine, 12, 4) +
            Copy(FileLine, 17, 4) +
            Copy(FileLine, 27, 3);
    end
    else if Pos('H DI', FileLine) > 0 then // If the keyword is H DIR
    begin
      SL.Add('H DI' +
            Copy(FileLine, 12, 4) +
            Copy(FileLine, 17, 4) +
            Copy(FileLine, 27, 8);
    end;
  end;
end;

This will create a stringlist with all of the codes and there data,
there is no COO because you did not specify what you wanted to extract from it.
0
 

Author Comment

by:Ham
Comment Utility
Thankyou, Ham.
0
 

Author Comment

by:Ham
Comment Utility
Hello,
  Now I have a slight variation because the search works fine - actually too fine - it finds every reference to the keyword and does not limit itself to the (for example) first 3 columns - for instance:
12345678901234567890
DIRECTION  234  345  <--- INTERESTED IN THIS LINE (DIR in First 3                           columns).

THE DIRECTION ARE... <--- BUT NO THIS LINE (AS IT IS NOT IN THE                           FIRST 3 COLUMNS).

Or for that matter is it possible to locate the keyword in keycolumns?

Thankyou, Ham.
0
 
LVL 1

Expert Comment

by:CFantin
Comment Utility
You can check the position of the text returned and therefor you will know where the text is in the line.

eg: (This is the check I am rewriting the ReadLn)

//LineRead is the line read from the file
//Check if the Position of 'DIR'  is at the beginning of the line
//and not anywhere in the line.
if Pos('DIR', LineRead) = 1 then
  ProcessTheLine;
 
Good luck
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

6 Experts available now in Live!

Get 1:1 Help Now