Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Text File reading

Posted on 1997-08-31
8
212 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
ID: 1343235
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
ID: 1343236
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
ID: 1343237
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Ham
ID: 1343238
You never replied
0
 
LVL 1

Accepted Solution

by:
CFantin earned 20 total points
ID: 1343239
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
ID: 1343240
Thankyou, Ham.
0
 

Author Comment

by:Ham
ID: 1343241
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
ID: 1343242
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
URL for downloading Google Chrome for Win XP 2 182
how to update exe applicatio from internet ? 6 86
Base1 Encode/Decode 3 81
DBCtrlGrid, Delphi, Scroll 8 37
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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