Results 1 to 15 of 15

Thread: Search & delete a string in text File

  1. #1

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Search & delete a string in text File

    Hi,

    How to search for a string in a text file and delete it.

    my search string is:

    {*test*}

    here * is one or more characters.

    Thanks in advance.
    CS

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Search & delete a string in text File

    You'll need to read the content of the file into a string and do the replacement and then write the string back.

    To do replacement with wildchars you probably want to use regular expressions.

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Search & delete a string in text File

    here's a non regexp version:
    VB Code:
    1. Dim sText As String, lPos As Long, lStart As Long, lEnd As Long
    2.  
    3.     Open "C:\file.txt" For Input As #1
    4.         sText = Input(LOF(1), #1)
    5.     Close #1
    6.  
    7.     lStart = 1
    8.     Do
    9.         lPos = InStr(lStart, sText, "test")
    10.         If lPos Then
    11.             lStart = InStrRev(sText, "{", lPos)
    12.             lEnd = InStr(lPos, sText, "}")
    13.             If lEnd > 0 And lStart > 0 Then
    14.                 sText = Left$(sText, lStart - 1) & Mid$(sText, lEnd + 1)
    15.             Else
    16.                 lStart = lPos + 4
    17.             End If
    18.         End If
    19.     Loop Until lPos = 0
    20.    
    21.     Open "C:\file.txt" For Output As #1
    22.         Print #1, sText
    23.     Close #1

  4. #4

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Search & delete a string in text File

    Thanks Bush.

    Is it faster and better way than regexp version?

    Can you post the regexp version also please?
    CS

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Search & delete a string in text File

    I don't know any regexp so I can't help you there.

    I also couldn't comment on the speed - if you want to do it really fast then you probably wouldn't use my code or regexp. The one thing I can say for certain is that the RegExp method will use a lot more memory.

  6. #6

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Search & delete a string in text File

    No problemo Bush. Your code helped me a lot. Thanks.

    I will wait for someone else to get some help on regexp version also.

    Thanks a lot.
    CS

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Search & delete a string in text File

    The point being that you could you use RegEx if you wanted to use wildcards, and since you stated you can use BM's example,
    then RegEx may not be required.

    Additionaly, why can't you use Replace() like Joacim mentioned??
    Load the file into a buffer, then Replace(), and save back out.

  8. #8

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Search & delete a string in text File

    Quote Originally Posted by Bruce Fox
    why can't you use Replace() like Joacim mentioned??
    Load the file into a buffer, then Replace(), and save back out.
    the search string is not constant. As mentioned in my post #1, I want to search *test* where * may be one or more characters. so i cannot use replace().
    Quote Originally Posted by Bruce Fox
    The point being that you could you use RegEx if you wanted to use wildcards, and since you stated you can use BM's example, then RegEx may not be required.
    Currently I use BM's code. I am also looking for code which uses Reg Expressions. so that I can learn something about RegExp.

    Thanks.
    CS

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Search & delete a string in text File

    In the example you provided (and as in BM's example) you can of course use Replace()!

    You can create a Sub and pass the search string to it.

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Search & delete a string in text File

    I never suggested that the Replace function should be used since it doesn't support wildchars. What I said was simply to do the replacement using regexp.

    Here's an example, it requires a reference to "Microsoft VBScript Regular Expressions"
    VB Code:
    1. Dim rx As RegExp
    2. Dim sText As String
    3.  
    4. 'Code to read the content of the file and store it
    5. 'in sText goes here...
    6. Set rx = New RegExp
    7. rx.Global = True 'replace all, set this to false to only replace the first match
    8. rx.IgnoreCase = True 'case insensitive search
    9. rx.Pattern = "({[\s\S]*?)test([\s\S]*?})"
    10. sText = rx.Replace(sText, "$1$2")
    11. 'code to write back the content of sText to the
    12. 'file goes here
    The above will remove the word "test" if it is somewhere inside curly brackets (or did you want to remove everything inside the brackets if the word "test" was found?).

  11. #11

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Search & delete a string in text File

    Thanks Joacim.
    Quote Originally Posted by Joacim Andersson
    did you want to remove everything inside the brackets if the word "test" was found?.
    Yes. I want to remove the entire search string including curly braces.

    The conditions are:

    1. The string should be withing curly braces.
    2. The string should have the word "test"

    I want to remove the entire string including curly braces, if it satisfies the above criterias. For example,
    Code:
    {/a \d test jdd}    -    This should be removed. it satisfies the criterias
    {/a \d tost jdd}    -    This should not be removed. it doesn't satisfies the criterias
    What are the advantages and disadvantages of using RegExp?

    Thanks.
    Last edited by cssriraman; Jul 11th, 2006 at 06:02 AM.
    CS

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Search & delete a string in text File

    OK, in that case simply change the last two lines to:
    VB Code:
    1. rx.Pattern = "{[\s\S]*?test[\s\S]*?}"
    2. sText = rx.Replace(sText, "")
    That will remove it all if the word "test" is somewhere inside the curly brackets (actually it will remove it if the word "testing" or something like that exists. Let me know if you need an exact word match).

    The main advantage of using regular expression is that you with a very few lines can search for (and replace) text with a complex pattern matching.

    The main disadvantage is that regex is a bit cryptic to say the least, even for a regex pro it takes a while to read and understand what it does. (Just make sure you write clear comments and you'll overcome that).

    Another advantage is that, in most cases, it runs much faster than using VBs own string manipulation functions.

    There are other disadvantages related to the MS implementation of regular expressions also since it doesn't cover all of them and even some documented wildcard characters doesn't work as expected. You also need another reference, but you're not allowed to distribute the file. People need to have it installed already (or download and install it from MS). But that's not a big problem since everyone that has MSH installed has it already and that comes with Windows, at least since Win98SE.

    I don't know if it uses more memory as Bush claimed. I actually don't think it does except for the fact that you need to create a new object which of course consumes a bit of memory.

    I personally found regexp to be invalueble when it comes to string parsing, especially when parsing larger text. Once you've learned how to use it I'm sure you will find it that way as well.

  13. #13

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Search & delete a string in text File

    Thanks a lot for the information.

    I will check this and come back.

    Thanks.
    Last edited by cssriraman; Jul 11th, 2006 at 05:15 PM.
    CS

  14. #14
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Search & delete a string in text File

    That sounds strange. I did a test for a text typed on several lines in a multiline textbox and that worked just fine. Which version of the MS VBScript Regular Expression library are you using?

    EDIT: Ehhh... Did you just change your previous post?

  15. #15

    Thread Starter
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Search & delete a string in text File

    I'm sorry. I got different outputs. So, I want to check again. I will come back to you, once I checked. That's why I edited my previous post.
    CS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width