How Can I Remove Any Line in a Text File That Contains a Specified String Value?

How Can I Remove Any Line in a Text File That Contains a Specified String Value?

  • Comments 9
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I remove any line in a text file that contains the string f0?

-- CJ

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CJ. Before we begin, we have a question for you: are you by any chance a serious player of online video games like EverQuest or World of Warcraft? Admittedly, none of the Scripting Guys play these games; we still find Pong to be a little over-stimulating for our tastes. However, we are absolutely fascinated by the underground economy that has sprung up around these “massively multiplayer online role-playing games.” For example, suppose you start to play EverQuest and you suddenly realize, “Dang, I need a 77 monk and a 55 druid.” What are you supposed to do then?

Now, if you’re stodgy, old-fashioned squares like the Scripting Guys, you’d probably just continue to play the game and try to earn a 77 monk and a 55 druid. Apparently, though, actually playing the game they spend all their time playing is too much effort for a lot people; instead, they just jet over to Ebay, where you can buy video game characters for the low, low price of $115:

70 monk with 720 aa's, very good character. Has gloves of coalesced flame, bone staff of wickedness, fiery staff of zha, many other great items. I can show him in game, he is currently on the cazic thule server. There is also a 55 druid, 53 ranger, 35 paladin, 35 enchanter, 33 berserker. Account comes with 260k and a few items worth over 100k. Once payment is received i will give the station name, password, and secret question.

And yes, that’s $115 in real money, not some sort of virtual EverQuest money.

Here’s another ad we stumbled upon:

This auction is for the time and energy spent to play this game and acquire the levels of the characters in the game. Only a sheet of paper containing information is being sold. [Italics added.] 75 Cleric on Luclin server. Original owner. Has many clickies that aren't included in the magelo. AoN / Spectre Mask / DE Mask / Wood Elf Mask and many other great clickies. 13k hp / 14.6k mana UNBUFFED http://eq.magelo.com/profile/727280

The asking price for the “sheet of paper containing information?” A mere $400. Also in real money.

Let’s face it: somehow, somewhere, the Scripting Guys became a bunch of old fogeys. $400 for a piece of paper? Not unless that piece of paper is a $1,000 bill.

And even then we’d have to think about it.

As we noted, the underground economy surrounding online video games has become huge; in 2002, one researcher estimated that EverQuest was the 77th richest “nation” on the face of the Earth. Admittedly, the Scripting Guys don’t fully understand why people would pay huge amounts of real money just to acquire new characters or new equipment for a video game. But then again, it’s not up to us to judge the motives behind the behaviors of other people; all we want to do is to get a piece of the action. Therefore, we have decided to offer everyone the deal of a lifetime: a Level 589 A5 script, one that has never been included in the magelo. The price for this script, which deletes all the lines in a text file that include the string f0? During this limited-time offering, you can get this script for just $3,985.

Plus shipping and handling.

Now, we know what you’re thinking, you’re thinking, “Wow, $3,985; that’s a lot of money to pay for a Level 589 A5 EverQuest script, especially when you consider the fact that there’s no such thing, and, even if there was, this script would be totally useless to anyone playing EverQuest.” Granted, that does sound a bit pricey, even to the Scripting Guys. (Although, as we noted, this has never been included in the magelo. Ever.) But we have a feeling you’ll change your mind after you learn how the script actually works.

To begin with, let’s pretend that you’re a 55 druid and you have a text file (C:\Scripts\Test.txt) that looks like this:

This line should stay.
This line (f0) should be deleted.
This line should also be deleted. (f0)
This line should not be deleted.
This line should remain.
f0: This last line should be deleted.

You’re absolutely right about that: no 55 druid could afford to be caught dead with a text file like this one, not with all those lines of text that contain the string value f0. But how are you, a mere 55 druid, supposed to delete each and every line that contains that string value, and only those lines that contain that string value?

Well, if you’re smart, and if you have $3,985, you’ll use a Level 589 A5 script like this one:

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForReading)

Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    If InStr(strLine, "f0") = 0 Then
        strNewContents = strNewContents & strLine & vbCrLf
    End If
Loop

objFile.Close

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForWriting)
objFile.Write strNewContents

objFile.Close

And yes, we’ll explain how this script works; for $3,985, that’s the least we can do. As you can see, we start out by defining a pair of constants, ForReading and ForWriting; we’ll need these two constants when we open the text file. (For better or worse, we have to open the file twice: once to read the current contents of the file into memory, and a second time to write the modified contents back to the file.) After defining our two constants we create an instance of the Scripting.FileSystemObject object, then use the following line of code to open the file C:\Scripts\Test.txt for reading:

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForReading)

You’re right: that probably is worth $3,985 right there. But we’re not done yet; far from it. Instead, once our file is open for reading we execute this block of code:

Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    If InStr(strLine, "f0") = 0 Then
        strNewContents = strNewContents & strLine & vbCrLf
    End If
Loop

Good question: what are we doing here? (No need to apologize; we wouldn’t expect a 55 druid to know what that block of code is for.) For starters, what we’re doing here is setting up a Do Until loop designed to run until we reach the end of the file. (Or, for all you 33 berserkers out there, until the file’s AtEndOfStream property is True.) Inside this loop we’re going to read the file line-by-line; that’s what this line of code (and the ReadLine method) is for:

strLine = objFile.ReadLine

As you can probably figure out for yourself, the first time through the loop we’re going to read the first line in the text file and store that value in the variable strLine. (In case you’ve forgotten, the first line in the text file is this: This line should stay.)

What we need to do now is determine whether or not the string value f0 appears anywhere within that line of text. How do we do that? Why, by using VBScript’s InStr function, of course:

If InStr(strLine, "f0") = 0 Then

The InStr function looks at a string value (like the variable strLine) and reports back the character positions where the target text (f0) is found. For example, if strLine was equal to 123f0, InStr would report back the value 4. Why? Because the target text begins in the fourth character position:

Position 1

Position 2

Position 3

Position 4

Position 5

1

2

3

f

0

And what if the target text can’t be found, as is the case with our first line of text? No problem; in that case, InStr returns a 0.

The fact that InStr returns a 0 if the target text can’t be found turns out to be pretty useful, at least for all us 55 druids. If InStr returns anything but a 0 that means that the target text was found somewhere in the string strValue; in turn, that means that this is a line of text that we need to discard. Because of that, we don’t do anything if we get back a value other than 0; we just ignore that line of text altogether. However, if we do get back a 0 (meaning that the target text could not be found) then we execute this line of code:

strNewContents = strNewContents & strLine & vbCrLf

Again, this should be pretty straightforward: all we’re doing here is assigning a value to a variable named strNewContents, a value made up of the following:

The current value of strNewContents (the current value, the first time through the loop, being an empty string).

The line of text we just read in from the text file (represented by the variable strLine).

A carriage return-linefeed (represented by the VBScript constant vbCrLf).

And then we loop around and repeat the process with the next line in the text file.

By the time we’ve reached the end of the file (OK, OK: the end of the stream) the variable strNewContents will be equal to the following, which just happens to be the contents of the text file minus any lines that contain the string f0:

This line should stay.
This line should not be deleted.
This line should remain.

That’s great but, for the moment anyway, the text file itself remains unchanged. But we can fix that. After closing the file (text files can be opened for reading or for writing, but not for both, at least not at the same time) we then immediately reopen the thing, this time for writing:

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForWriting)

At that point all we have to do is call the Write method and write the modified contents (the value of strNewContents) to the text file:

objFile.Write strNewContents

After that we close the text file one last time and we’re done.

That should do it, CJ; just send us a check for $3,985 and we’ll call it good. Oh, and in case you’re wondering, the answer is no, at this time the Scripting Guys do not offer tours of virtual worlds like EverQuest.

But don’t fret; amazing enough, there are travel agencies that do offer guided tours of virtual worlds like EverQuest.

Maybe we Scripting Guys need to give Pong another chance. We can’t afford to get any more fogeyish than we already are.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • This still leaves blank lines.  Any way to fix that?

  • Please disregard the comment.  I simply hadn't noticed that the blank lines already exist.  However, how would one modify the script to remove blank lines?

  • this code works fine when the file size ius less.. but the code gets stuck when the file size is huge

    ..any idea?

  • Hi, I have a file size of 800 MB, I need to search and delete specific lines in the text file based on input given from excel sheet.. the above works fine when the file size is small but seems to take long time when used for large text files. Is there any way we can reduce the time. ?

  • I altered the script to the following to write each line as it is identified.  This keeps the buffer from growing out of control.  I am using a 176GB file.

    Const ForReading = 1

    Const ForWriting = 2

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFSO.OpenTextFile("C:\Scripts\input_file.txt", ForReading)

    Set objOutFile = objFSO.OpenTextFile("C:\Scripts\output.txt", ForWriting)

    Do Until objFile.AtEndOfStream

       strLine = objFile.ReadLine

       If InStr(strLine, "f0") = 0 Then

    objOutFile.WriteLine(strLine)

       End If

    Loop

    objFile.Close

    objOutFile.Close

  • Very Very useful :-)

  • Wonderful explanation!!! Cant go wrong.

  • Works well ! Thank you guys.. A well directed explanation too..

  • If i have many words, and want to remove the line, how to do it? For above example "also" and "not" will be remove the line. Thanks.