# How to split a TXT file into parts based on line content?



## j_hallgren (Mar 7, 2005)

Got a situation that I'm looking for advice on: 
I have a TXT file that is a log file that has various types of entries in it...
Each entry is a single line of text, and the entry type is a single word in a fixed place in that line.

My wish is to have some utility or easy process that would allow me to extract those lines that contain certain text into one file, while putting the remaining text lines into a second file.
I have Office 2000 if that helps any...


----------



## JohnWill (Oct 19, 2002)

A sample of the file in question would probably be useful for anyone trying to answer the question.

I'd probably write a simple C program to do it.

I suspect this could also be done with some scripting languages, certainly Perl could do the job.


----------



## WhitPhil (Oct 4, 2000)

Are you looking for a one time solution (ie a quick and dirty), or is this something you will be doing on a regular basis.


----------



## Rollin_Again (Sep 4, 2003)

What you are wanting can easily be done with a VBS Script. Post your sample text file and I can help out with some code.

An easier option would be to open the text file in Excel and manipulate it there. You could use a macro (VBA) in Excel or take advantage of some of the built in sorting/autofiltering to accomplish what you want before re-saving as a text file.

Regards,
Rollin


----------



## j_hallgren (Mar 7, 2005)

Thanks much for replies! 
Rollin & John: I would need to do this same process on occasion, maybe once/twice a month using current cummulative log file as input.
The present log file is about 600KB and has about 5200 lines of text currently.

Each line of text begins with "" followed by either "NORMAL" or "ERR: " and then a varying amount of text (approx 40-80 chars) ending with "
" followed by a carriage return.

My goal is to split the "NORMAL".. from the "ERR: "... lines so I can keep the "NORMAL" lines for reference, and upload the trimmed file, but remove the "ERR" lines for futher investigation.

I had thought about using Excel, but wanted to check for other solutions also.

I prefer not to publicly post a sample as entries contain visitor names and IP addresses, but hopefully the info above will help. Was hoping there might be some existing free/donation-ware that would lend it self to this task.


----------



## Rollin_Again (Sep 4, 2003)

Copy the following code into a text file (notepad) and then re-name with the extension *.VBS*

Before renaming the file make sure to do the following:

Change the file path and name of the source text file that contains all the records ("C:\SourceFile.txt")

Save your source text file with the same name and file location as above.

Change the file path and name of the file to be created containing the errors ("C:\Error.txt")

Change the file path and name of the file to be created containing normal records ("C:\Normal.txt")

When you run the script it will read the source file and split the records into two seperate files.

Hope this helps!


```
Dim objSource
Dim strLine
Dim objError
Dim objNormal
Dim objFSO
Const ForReading = 1


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSource = objFSO.OpenTextFile("C:\SourceFile.txt", ForReading)
Set objError = objFSO.CreateTextFile("C:\Error.txt", True)
Set objNormal = objFSO.CreateTextFile("C:\Normal.txt", True)

Do Until objSource.AtEndOfStream
strLine = UCase(objSource.ReadLine)
If InStr(1, strLine, "<TT>NORMAL") > 0 Then
objNormal.WriteLine (strLine)
ElseIf InStr(1, strLine, "<TT>ERR") > 0 Then
objError.WriteLine (strLine)
End If

Loop

objError.Close
objNormal.Close
objSource.Close

Set objSource = Nothing
Set objError = Nothing
Set objNormal = Nothing
Set objFSO = Nothing
```
Regards,
Rollin


----------



## Squashman (Apr 4, 2003)

A simple grep would do the trick as well.


----------



## j_hallgren (Mar 7, 2005)

Rollin, that VBS script was 99.9% perfect! Why not 100%? Well, I had to remove the "Ucase( )" as I needed to keep the line data in mixed case, and the test string is always as I'd stated...I basically understood what the code does, but it'd have taken me quite a while for me to learn enough to code it my self, so THANKS VERY MUCH!

I did set the values of the input and output filenames to my specific values, and it worked just fine, after the Ucase mod, so now I can eliminate the existing 50KB of errors from the file and keep it clean in the future.

IF my website wasn't getting SO many spam attempts, I wouldn't need this, but now I can better spot the IP patterns and add blocking code for those groups, mostly all located overseas. 

Again, I DO thank you for your prompt reply and code! 

Squashman: Isn't "grep" a Unix type thing? I'm using W2K...


----------



## JohnWill (Oct 19, 2002)

Grep is available for Windows.


----------



## Squashman (Apr 4, 2003)

JohnWill said:


> Grep is available for Windows.


I know, I have all the Unix Utils loaded on my PC at work. Been writing several scripts of late on my PC to fix our customers data before we move it to the mainframe.

No offense to Rollin, but I could have done the same thing with two lines using Grep. I have been wanting to learn Vbscripting for the longest time, but time is something I haven't had alot of.


----------



## JohnWill (Oct 19, 2002)

You have to admit, you can do a lot more with VBS Script than with Grep, this just happens to be something that Grep would have done.


----------



## JohnWill (Oct 19, 2002)

Let's see, something along those lines? 

grep -i "^TTERR:" [_file/path specification_] >errlines.txt
grep -i "^TTNORMAL" [_file/path specification_] >normallines.txt


----------



## Squashman (Apr 4, 2003)

JohnWill said:


> You have to admit, you can do a lot more with VBS Script than with Grep, this just happens to be something that Grep would have done.


I totally agree. But Sometimes those good ole unix utilities make things much more simple.

My friend actually wrote an entire Backup Utility using nothing but Bash Scripts. It is totally Menu Driven. They used it as a Backup solution for the clients they did support for. It was a total backup server. It was quite slick.


----------



## zesi (Aug 5, 2007)

You can download TextMaster utility from www.textmaster.ca which is designed to solve problem you have using SQL directly on the text file. Do the following:

1 - check your file using file check feature;
2 - create connection using TextMaster terminology (describe file)
3 - open query builder and create insert statemnt which will copy lines from original file into new file but only lines whick contain value(s) specified in where clause
4 - save query for the future use

NOTE: you can use delete statement instead of insert statement as well and filter out lines you do not want. Make sure that you have backup of the file before you do it in case that you need it later.

You will have 30 days to decide to purchase it or not


----------



## TheOutcaste (Aug 8, 2007)

Good ole DOS works too:

find /i "Normal" < c:\source.txt > normal.txt
find /i /v "Normal" < c:\source.txt > err.txt

HTH

Jerry


----------



## Squashman (Apr 4, 2003)

TheOutcaste said:


> Good ole DOS works too:
> 
> find /i "Normal" < c:\source.txt > normal.txt
> find /i /v "Normal" < c:\source.txt > err.txt
> ...


Good Call. Would be a simple solution for this problem.

The nice thing about grep though is that it can take a filename as the string of arguements you want to search for.


----------



## zesi (Aug 5, 2007)

You can download TextMaster from www.textmaster.ca and do what you have to the on the following way:
1 - create connection (describe your file) so that everything till start of the value is one field and rest of the line second field 
2 - save description so you can do it again
3 - create "delete" statement and in where clause specify what you do not want your second field to start with

Copy file under different name and repeat steppes above but this time delete everything you want second field to start with.
TextMaster has excellent support. You can a few lines to them and most probably they will send you template and queries.


----------



## Squashman (Apr 4, 2003)

Seriously. Do you have to keep rehashing this thread and advertising your software. Go away if you don't have anything else to contribute to this forum.


----------

