Hello sokowicz-ga
You have not specified the language that you required the script to be
written in so I have written it in Visual Basic Script Language. This
allows it to be run simply on any modern Microsoft Windows system (if
you require it for another operating system I can easily convert it to
a small Perl script for you). I have tried to make the script as
user-friendly as possible by commenting it throughout.
What the script does: it reads line-by-line a source file called
"source.txt" from the same directory as the script. If the line in
the file contains "good test", "bad test" or "invalid test" it
remembers the line for later in the script. After the script has
looked at every line in the source file it outputs the "remembered"
lines into three files "good.txt", "bad.txt" and "invalid.txt" which
are created in the same directory as the script.
To create the script:
1) Open a new text file in Notepad
2) Copy the following lines into the new text file:
' set up an object for the files
Dim MyStream, FSys
' read in the source file
Set FSys = CreateObject("Scripting.FileSystemObject")
Set MyStream = FSys.OpenTextFile("source.txt",1,False,False)
While MyStream.AtEndOfStream = False
TheLine = MyStream.ReadLine
If InStr(TheLine,"good test") Then
good = good & TheLine & vbCrLf
End If
If (InStr(TheLine,"bad test") > 0 ) Then
bad = bad & TheLine & vbCrLf
End If
If (InStr(TheLine,"invalid test") > 0 ) Then
invalid = invalid & TheLine & vbCrLf
End If
Wend
' write good file
Set MyStream = FSys.CreateTextFile("good.txt")
MyStream.Write good
' write bad file
Set MyStream = FSys.CreateTextFile("bad.txt")
MyStream.Write bad
' write invalid file
Set MyStream = FSys.CreateTextFile("invalid.txt")
MyStream.Write invalid
' Remove objects
Set MyStream = Nothing
Set FSys = Nothing
3) Save the file as tests.vbs where you store your "source.txt" file.
4) Double-click on the tests.vbs file to run it. (Note at this point
your virus checker may complain, to run the script you must tell your
virus checker to allow this script.)
5) 3 text files will be created in the directory called "good.txt",
"bad.txt" and "invalid.txt" with the results you require.
If you have any further queries on this subject please ask for
clarification and I will do my best to respond swiftly. |
Request for Answer Clarification by
sokowicz-ga
on
18 Oct 2004 07:21 PDT
very great and extremly quick :)
PS: Something else, i try to change my username now for some month but
nobody was abled to help me, can you may change it to anything else no
matter what?
best regards and thanks..
|
Request for Answer Clarification by
sokowicz-ga
on
18 Oct 2004 07:47 PDT
another thing...can you may also tell me how to modify the script so
it will cut out every line and put it into a new .txt file name like
the starting value (100001.txt,100002.txt,...).
That would be great, thanks!
|
Clarification of Answer by
palitoy-ga
on
18 Oct 2004 08:12 PDT
Sorry but I cannot help you with the username issue as I have no
control over those, if you email answers-editors@google.com they may
be able to help you with this problem.
For your second query do you mean you require a new text file for
every line as well as the good/bad/invalid idea?
|
Request for Answer Clarification by
sokowicz-ga
on
19 Oct 2004 09:42 PDT
yes in the new text file should be the whole line,
another and finally last question would be how to make the script put
the bad into bad.txt and good AND invalid into good.txt ?
Thanks and best regards!
|
Clarification of Answer by
palitoy-ga
on
19 Oct 2004 10:09 PDT
Hi sokowicz-ga
The solution for the invalid and good going into good.txt is:
' set up an object for the files
Dim MyStream, FSys
' read in the source file
Set FSys = CreateObject("Scripting.FileSystemObject")
Set MyStream = FSys.OpenTextFile("source.txt",1,False,False)
While MyStream.AtEndOfStream = False
TheLine = MyStream.ReadLine
If ((InStr(TheLine,"good test") > 0) Or (InStr(TheLine,"invalid
test") > 0 )) Then
good = good & TheLine & vbCrLf
End If
If (InStr(TheLine,"bad test") > 0 ) Then
bad = bad & TheLine & vbCrLf
End If
Wend
' write good file
Set MyStream = FSys.CreateTextFile("good.txt")
MyStream.Write good
' write bad file
Set MyStream = FSys.CreateTextFile("bad.txt")
MyStream.Write bad
' Remove objects
Set MyStream = Nothing
Set FSys = Nothing
For your other question the script should be:
' set up an object for the files
Dim MyStream, FSys
' read in the source file
Set FSys = CreateObject("Scripting.FileSystemObject")
Set MyStream = FSys.OpenTextFile("source.txt",1,False,False)
While MyStream.AtEndOfStream = False
TheLine = MyStream.ReadLine
If (InStr(TheLine," ") > 0 ) Then
' get filename
filename = split(TheLine," ")
' write file
Set MyStream2 = FSys.CreateTextFile(filename(0) + ".txt")
MyStream2.Write TheLine
End If
Wend
' Remove objects
Set MyStream = Nothing
Set MyStream2 = Nothing
Set FSys = Nothing
|
Request for Answer Clarification by
sokowicz-ga
on
19 Oct 2004 10:33 PDT
Thank you very much, i'll rate your answere with 5 stars asap.
If you do not mind I may as, which programming languages did you learn
and for how long?
Thanks and best regards!
|
Clarification of Answer by
palitoy-ga
on
19 Oct 2004 11:10 PDT
I can program in a number of computer languages. Visual Basic, Perl,
C++ and Python are my favourites but I can remember bits of a few
other dead or dying languages too! I also enjoy scripting in PHP and
HTML. I find that once you learn one language the others are
reasonably easy to pick up to do simple tasks. I wouldn't say I was
an expert in any as I just float from language to language writing in
whichever I think is the easiest to achive the task. I guess I've
been writing programs seriously for about 5 years... maybe one day
I'll make a living from it!
|
Request for Answer Clarification by
sokowicz-ga
on
19 Oct 2004 11:26 PDT
okay thanks you, i would be glad to have a talk with you, check your mail :)
|
Clarification of Answer by
palitoy-ga
on
20 Oct 2004 01:50 PDT
Thanks for the 5-star rating, it is appreciated!
|