Google Answers Logo
View Question
 
Q: Small Script needed ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Small Script needed
Category: Computers > Programming
Asked by: sokowicz-ga
List Price: $15.00
Posted: 18 Oct 2004 05:43 PDT
Expires: 17 Nov 2004 04:43 PST
Question ID: 416340
Hello,
im searching for a script or a program that will cut out special lines
from a .txt file and write it into new .txt files. Here an example how
the source file will look like:

100001 : test:000C : invalid test
100002 : test:1707 : good test
100003 : test:0001 : bad test
100004 : test:0001 : bad test
100005 : test:1707 : good test
100006 : test:0001 : bad test
100007 : test:000C : invalid test

and now i want the program or the script to put every (whole) line
with "good test", every (whole) line with "bad test" and every (whole)
line with "invalid test" into a new file, in that case into 3
different files (good.txt,bad.txt,invalid.txt)

Thanks and best regards!
Answer  
Subject: Re: Small Script needed
Answered By: palitoy-ga on 18 Oct 2004 06:41 PDT
Rated:5 out of 5 stars
 
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!
sokowicz-ga rated this answer:5 out of 5 stars
thanks alot!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy