?
Solved

Need to read a text file line by line, then write lines with a certain criteria to a save file

Posted on 2015-01-27
8
Medium Priority
?
94 Views
Last Modified: 2015-01-29
I have very large text files with data on a line by line basis.  I need to scan down through the file looking for a keyword that could be a trigger, then write that line out to a holding file.  When the source file reaches EOF, then close it and the target file.

I keep having the open files step on each other, and I'm not sure how to specify reading from one, then writing to the other.  Can I have both open at the same time?
0
Comment
Question by:Mike Caldwell
[X]
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
  • 5
  • 3
8 Comments
 
LVL 57

Expert Comment

by:Bill Prew
ID: 40573542
Can you share what you have written so far?

What is the "trigger" criteria?

What data goes into the holding file?

~bp
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 40573549
OK, but it is very primative, and won't run due to errors.

dim filesys, filetxt
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile("G:\API Stuff\Sample Reference List.txt", ForReading, True)
set Outfile = filesys.OpenTextFile("G:\API Stuff\Sample out.txt",ForWriting)

Do until EOF
strLine = filetxt.readLine
Outfile.WriteLine = strLine
Loop

filetxt.Close
Outfile.CLose

Open in new window


The trigger is when "[Application]" is found on a line.  Then I go down a couple more lines and look for another trigger, such as "[FIELD2]", and write the line were FIELD2 was found into the out file.
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 40574198
Is this an INI type file by any chance?

Can you provide a sample of the input file?

~bp
0
Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

 
LVL 1

Author Comment

by:Mike Caldwell
ID: 40574692
stdClass Object
(
    [APPLICATION] => stdClass Object
        (
            [APNMB] => 12345678
            [PBNUM] => 20090108066
            [PBDAY] => 2009-04-30
            [PANUM] =>
            [PADAY] =>
            [EPDAY] => 2006-06-14
            [FPDAY] => 2006-03-01
            [EXPUN] =>
            [ATYPE] => Utility
            [ASTAT] => Abandoned -- Failure to Respond to an Office Action
            [NSTAT] => App Abandoned
            [STDAY] => 2012-08-27
            [EXAMN] => STANFORD, CHRISTOPHER J
            [CONFM] => 8142
            [DOCKN] => OP100000929
            [ARTUN] => 2887
            [CLASS] => 235
            [SBCLS] => 472.010
     }
}

Data like this would be repeated, perhaps hundreds of time.  I just need to first open the file, look for the first trigger, then the second trigger, say DOCKN, then copy out

      [DOCKN] => OP100000929

to the target file.
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 40576574
Okay, working it.  Do you want to only write out the first match in the file, or every match?

~bp
0
 
LVL 57

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 40576645
Give this a try. Save as a VBS, adjust the filenames, and give a try.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Define input and output file names
Const Infile = "B:\EE\EE28604950\filein.txt"
Const Outfile = "B:\EE\EE28604950\fileout.txt"

' Define trigger strings to look for
Const Trigger1 = "[APPLICATION]"
Const Trigger2 = "[DOCKN]"

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Open input and output files
Set oInfile = oFSO.OpenTextFile(Infile, ForReading, False, TriStateUseDefault)
Set oOutfile = oFSO.OpenTextFile(Outfile, ForWriting, True)

' Reset trigger string found flags
bFoundTrigger1 = False
bFoundTrigger2 = False

' Read input file line by line
Do While Not oInfile.AtEndOfStream
   ' Read the next line
   sLine = oInfile.ReadLine

   ' If we have found the first trigger, then look for the second trigger text
   If bFoundTrigger1 And (InStr(1, sLine, Trigger2, vbTextCompare) > 0) Then
      ' Found second trigger, write to output file
      oOutfile.WriteLine sLine
      bFoundTrigger1 = False
      bFoundTrigger2 = True
   End If

   ' If we find the first trigger then set flags to indicate that
   If InStr(1, sLine, Trigger1, vbTextCompare) > 0 Then
      bFoundTrigger1 = True
      bFoundTrigger2 = False
   End If
Loop

' Cleanup and end
oInfile.Close
oOutfile.Close
Set oInfile = Nothing
Set oOutfile = Nothing
Set oFSO = Nothing

Open in new window

~bp
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 40577945
Perfect Bill, thanks.  I can figure out my loops, inner loops, etc, so I've got it from here.  Thanks much.
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 40578841
Very welcome, glad that helped.

~bp
0

Featured Post

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

719 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