mbt masai
 
Welcome !
         

                                
After experiencing a lot of down time, We decided to move this site to CrystalTech.com. CrystalTech.com is powered by only the finest Windows servers providing the best performance, reliability, and value anywhere.

 Need help with a text editing script, find/replace from a list of variables

Change Page: < 12 | Showing page 2 of 2, messages 21 to 33 of 33
Author Message
ebgreen

  • Total Posts : 8090
  • Scores: 95
  • Reward points : 0
  • Joined: 7/12/2005
  • Status: online
Re:Need help with a text editing script, find/replace from a list of variables Tuesday, March 09, 2010 8:51 AM (permalink)
0
Changing this line:

If strNewLine <> strLine Then

to this:

If strNewLine <> strLine OR Instr(strLine, ":") > 0 Then

Should take care of the colon issue. What is the structure of the other line to keep. Could you show a sample?
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm
#21
    emailsbecker

    • Total Posts : 47
    • Scores: 0
    • Reward points : 0
    • Joined: 2/28/2010
    • Status: offline
    Re:Need help with a text editing script, find/replace from a list of variables Tuesday, March 09, 2010 9:10 AM (permalink)
    0
    Ok, I got it.  I changed the second part to this:

    For Each strLine in Split(oFSO.OpenTextFile("C:\scripts\Data-ShIntDescTest.txt").ReadAll(), vbCrLf)
        For Each strNewName in dicReplacements.Keys()
            strNewLine = Replace(strLine, dicReplacements(strNewName), strNewName)
            If strNewLine <> strLine Then
                oOutFile.WriteLine strNewLine
                Exit For
            End If
            If InStr(strLine, "hostnamekeep") <> 0 Then
                oOutFile.WriteLine strLine
                Exit For
            End If
            If InStr(strLine, ":") <> 0 Then
                oOutFile.WriteLine strLine
                Exit For
            End If
        Next
    Next
    oOutFile.Close()

    I'm sure there was probably a way to do that with less code, but it works.
    #22
      ebgreen

      • Total Posts : 8090
      • Scores: 95
      • Reward points : 0
      • Joined: 7/12/2005
      • Status: online
      Re:Need help with a text editing script, find/replace from a list of variables Tuesday, March 09, 2010 9:13 AM (permalink)
      0
      Works is what matters.
      "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
      Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
      http://www.visualbasicscript.com/m_47117/tm.htm
      #23
        emailsbecker

        • Total Posts : 47
        • Scores: 0
        • Reward points : 0
        • Joined: 2/28/2010
        • Status: offline
        Re:Need help with a text editing script, find/replace from a list of variables Tuesday, March 09, 2010 9:22 AM (permalink)
        0
        Ah, I just saw the post above my last one.  I didn't even see that when I posted my fix.  But yeah, as long as it works.  Thanks for all the help :)
        #24
          emailsbecker

          • Total Posts : 47
          • Scores: 0
          • Reward points : 0
          • Joined: 2/28/2010
          • Status: offline
          Re:Need help with a text editing script, find/replace from a list of variables Wednesday, March 10, 2010 8:02 AM (permalink)
          0
          Looks like I spoke too soon.  I't not working as expected.  I think the error has to do with this line:

              strNewLine = Replace(strLine, dicReplacements(strNewName), strNewName)

          I'm thinking my earlier hunch was correct about it searching for the bad string and then replacing it with the same string.  I'm going to see if I can get it figured out.
          #25
            emailsbecker

            • Total Posts : 47
            • Scores: 0
            • Reward points : 0
            • Joined: 2/28/2010
            • Status: offline
            Re:Need help with a text editing script, find/replace from a list of variables Wednesday, March 10, 2010 10:21 AM (permalink)
            0
            I give up.  I've spent hours and I can't find anything that helps me understand how the scripting dictionary works with the replace command.  I tried rewriting the script from scratch using while loops and couldn't get that to work either.  Everyone else in my department has gone home ... time to pack it up.  Hopefully you'll post something tonight ... I'll check tomorrow.
            #26
              ebgreen

              • Total Posts : 8090
              • Scores: 95
              • Reward points : 0
              • Joined: 7/12/2005
              • Status: online
              Re:Need help with a text editing script, find/replace from a list of variables Thursday, March 11, 2010 3:39 AM (permalink)
              0
              Sometimes when a discussion has been going on for a while like this, it helps to do a reset. Could you please:

              1) Post the code that you are currently running?
              2) Post a sample of the lines in the replacements file? The lines that control old name/new name replacement.
              3) Explain what behavior you are currently seeing when you run what you have posted?
              "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
              Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
              http://www.visualbasicscript.com/m_47117/tm.htm
              #27
                emailsbecker

                • Total Posts : 47
                • Scores: 0
                • Reward points : 0
                • Joined: 2/28/2010
                • Status: offline
                Re:Need help with a text editing script, find/replace from a list of variables Friday, March 12, 2010 9:42 AM (permalink)
                0
                Here's what I have:
                 
                      Option Explicit
                     Dim dicReplacements : Set dicReplacements = CreateObject("Scripting.Dictionary")
                     Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
                     Dim oOutFile : Set oOutFile = oFSO.CreateTextFile("C:\Scripts\ChangedDescForQC.txt")
                     Dim strLine
                     Dim strNewLine
                     Dim strNewName     For Each strLine in Split(oFSO.OpenTextFile("C:\Scripts\Data-OldHostnamesToNew.txt").ReadAll(), vbCrLf)
                         dicReplacements.Add Split(strLine, ",")(0), Split(strLine, ",")(1)
                     Next     For Each strLine in Split(oFSO.OpenTextFile("C:\scripts\Data-EditedShIntDesc.txt").ReadAll(), vbCrLf)
                         For Each strNewName in dicReplacements.Keys()
                             strNewLine = Replace(strLine, dicReplacements(strNewName), strNewName)             If strNewLine <> strLine Then
                                 oOutFile.WriteLine strNewLine
                                 Exit For
                             End If             If InStr(strLine, "hostname") <> 0 Then
                                 oOutFile.WriteLine strLine
                                 Exit For
                             End If             If InStr(strLine, "host-name") <> 0 Then
                                 oOutFile.WriteLine strLine
                                 Exit For
                             End If             If InStr(strLine, ":") <> 0 Then
                                 oOutFile.WriteLine strLine
                                 Exit For
                             End If             If strLine = "" Then
                                 oOutFile.WriteLine strLine
                                 Exit For
                             End If         Next
                     Next
                     oOutFile.Close()     

                 
                Here's an example of the file that's being searched through and fixed:
                 

                Gi1/1 description NAME-55-2950-SWT1(Gi0/1)(MGMT)
                Gi1/21 description DAC-NAME53-MTRO-SWT6(SFP-C)::NAME53-2550-SAL1(6.1)
                Gi1/43.1040 description SOME-HIGH-SCHOOL:249-001i

                As an example, "NAME-55-2950-SWT1(Gi0/1)(MGMT)" needs to be changed to "NAME55-SWT1(Gi0/1)(MGMT)" and "NAME53-2550-SAL1(6.1)"  will become "NAME53-SAL1(6.1)".  Between the lists of each device's interface descriptions there is a line with that device's hostname.  I've gone through the file and put "hostname" or "host-name" in front of those and I use the "If InStr(strLine, "hostname") <> 0 Then" line to catch those and keep them because otherwise if the program comes across a hostname that is already correct it won't change it, and then because it wasn't changed it would dump it.
                 
                Currently the program runs through and doesn't seem to actually replace any of the bad hostnames because there are a lot of instances where improper hostnames are still showing up in the output.  The only thing I can think of is that it's running through the loop in the wrong way.
                 
                Say I have this description line:
                 

                g1/47 description badhostname1::badhostname2

                 
                The way the script currently runs I'll have these 2 results in the output file:
                 


                g1/47 description goodhostname1::badhostname2
                g1/47 description badhostname1::goodhostname2

                 
                Maybe this is all that's happening, and I'm confused because I'm still seeing bad hostnames in the descriptions.
                #28
                  emailsbecker

                  • Total Posts : 47
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 2/28/2010
                  • Status: offline
                  Re:Need help with a text editing script, find/replace from a list of variables Monday, March 15, 2010 5:20 AM (permalink)
                  0
                  What do you think about this... it might be memory heavy, but I think it could work.

                  What if I create a matrix that has 4 columns:

                  Hostname, interface (ie g1/47), description badhostname1::badhostname2, flag

                  The loop would check each row of the array against the badhostname/goodhostname variable, and save the result back into the array (if a change is made, set the flag to 1, if multiple changes are made leave the flag set to 1).  Run through the array repeatedly until all hostnames have been corrected.  Then save the results to a file by going through the array and saving any line that has the flag set to 1.

                  This would allow multiple changes to the same description line but result in only 1 line being output per changed interface.

                  Thoughts? Advice?
                  <message edited by emailsbecker on Monday, March 15, 2010 5:22 AM>
                  #29
                    ebgreen

                    • Total Posts : 8090
                    • Scores: 95
                    • Reward points : 0
                    • Joined: 7/12/2005
                    • Status: online
                    Re:Need help with a text editing script, find/replace from a list of variables Monday, March 15, 2010 5:28 AM (permalink)
                    0
                    I still think the basic approach we started with would work. You never showed an example of your replacements file. The file that has the bad name matched with the name that should replace it. Based on what you did show, it should look something like this:

                    NAME-55-2950-SWT1(Gi0/1)(MGMT),NAME55-SWT1(Gi0/1)(MGMT)
                    NAME53-2550-SAL1(6.1),NAME53-SAL1(6.1)

                    Or each row is reversed, I forget which way we had the logic. Regardless you need a file that lists each bad name matched to the good name that would replace it.
                    "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
                    Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
                    http://www.visualbasicscript.com/m_47117/tm.htm
                    #30
                      emailsbecker

                      • Total Posts : 47
                      • Scores: 0
                      • Reward points : 0
                      • Joined: 2/28/2010
                      • Status: offline
                      Re:Need help with a text editing script, find/replace from a list of variables Monday, March 15, 2010 8:57 AM (permalink)
                      0
                      That's pretty close.  It's like this:

                      NAME-55-2950-SWT1,NAME55-SWT1
                      NAME53-2550-SAL1,NAME53-SAL1

                      The other parts are references to specific ports, which could vary.  For example different lines might say  
                       
                      NAME53-2550-SAL1(2.1)
                      NAME53-2550-SAL1(3.1)
                      NAME53-2550-SAL1(4.1)
                       
                      ...where the hostname needs to change, but without touching the port number.
                      <message edited by emailsbecker on Monday, March 15, 2010 8:58 AM>
                      #31
                        ebgreen

                        • Total Posts : 8090
                        • Scores: 95
                        • Reward points : 0
                        • Joined: 7/12/2005
                        • Status: online
                        Re:Need help with a text editing script, find/replace from a list of variables Monday, March 15, 2010 12:49 PM (permalink)
                        0
                        Ok, tomorrow I will try to take your examples and come up with some working code.
                        "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
                        Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
                        http://www.visualbasicscript.com/m_47117/tm.htm
                        #32
                          ebgreen

                          • Total Posts : 8090
                          • Scores: 95
                          • Reward points : 0
                          • Joined: 7/12/2005
                          • Status: online
                          Re:Need help with a text editing script, find/replace from a list of variables Thursday, March 18, 2010 2:45 AM (permalink)
                          0
                          Sorry I haven't gotten to this sooner. I have some code that just demonstrates the name replacement since I think that is the biggest hurdle right now. I also don't have it writing out to a file, I'm just using echos to show what it is doing. I just wanted to present the logic and you can work it into your script however you see fit.

                          First, I have a file named Data-OldHostnamesToNew.txt. It contains this:

                          NAME-55-2950-SWT1,NAME55-SWT1
                          NAME53-2550-SAL1,NAME53-SAL1


                          Next I have a file named Data-EditedShIntDesc.txt. It contains this:

                          Gi1/1 description NAME-55-2950-SWT1(Gi0/1)(MGMT)
                          Gi1/21 description DAC-NAME53-MTRO-SWT6(SFP-C)::NAME53-2550-SAL1(6.1)
                          Gi1/43.1040 description SOME-HIGH-SCHOOL:249-001i

                          Then I have this script:

                          Option Explicit
                          Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
                          Dim dicReplacements : Set dicReplacements = CreateObject("Scripting.Dictionary")
                          Dim arrLines : arrLines = Split(oFSO.OpenTextFile("C:\Temp\Data-OldHostnamesToNew.txt").ReadAll(), vbCrLf)
                          Dim strLine
                          Dim strOldName
                          Dim strNewLine

                          For Each strLine in arrLines
                              if strLine <> "" Then
                                  dicReplacements.Add Split(strLine, ",")(0), Split(strLine, ",")(1)
                              End If
                          Next

                          For Each strOldName in dicReplacements.Keys()
                              WScript.Echo "OLD NAME = " & strOldName & vbTab & "MATCHING NEW NAME = " & dicReplacements(strOldName)
                          Next

                          WScript.Echo ""
                          WScript.Echo "**************"
                          WScript.Echo ""

                          arrLines = Split(oFSO.OpenTextFile("C:\Temp\Data-EditedShIntDesc.txt").ReadAll(), vbCrLf)
                          For Each strLine In arrLines
                              WScript.Echo "CHECKING: " & strLine
                              For Each strOldName in dicReplacements.Keys()
                                  If InStr(strLine, strOldName) > 0 Then
                                      WScript.Echo vbTab & "I found " & strOldName & " in this line. Replacing it now"
                                      strNewLine = Replace(strLine, strOldName, dicReplacements(strOldName))
                                      WScript.Echo vbTab & "NEW LINE: " & strNewLine
                                      Exit For
                                  End If
                              Next
                          Next


                          The output from this script is:

                          OLD NAME = NAME-55-2950-SWT1    MATCHING NEW NAME = NAME55-SWT1
                          OLD NAME = NAME53-2550-SAL1     MATCHING NEW NAME = NAME53-SAL1

                          **************

                          CHECKING: Gi1/1 description NAME-55-2950-SWT1(Gi0/1)(MGMT)
                                  I found NAME-55-2950-SWT1 in this line. Replacing it now
                                  NEW LINE: Gi1/1 description NAME55-SWT1(Gi0/1)(MGMT)
                          CHECKING: Gi1/21 description DAC-NAME53-MTRO-SWT6(SFP-C)::NAME53-2550-SAL1(6.1)
                                  I found NAME53-2550-SAL1 in this line. Replacing it now
                                  NEW LINE: Gi1/21 description DAC-NAME53-MTRO-SWT6(SFP-C)::NAME53-SAL1 (6.1)
                          CHECKING: Gi1/43.1040 description SOME-HIGH-SCHOOL:249-001i
                          CHECKING:
                          CHECKING:
                          "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
                          Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
                          http://www.visualbasicscript.com/m_47117/tm.htm
                          #33

                            Online Bookmarks Sharing: Share/Bookmark
                            Change Page: < 12 | Showing page 2 of 2, messages 21 to 33 of 33

                            Jump to:

                            Current active users

                            There are 0 members and 1 guests.

                            Icon Legend and Permission

                            • New Messages
                            • No New Messages
                            • Hot Topic w/ New Messages
                            • Hot Topic w/o New Messages
                            • Locked w/ New Messages
                            • Locked w/o New Messages
                            • Read Message
                            • Post New Thread
                            • Reply to message
                            • Post New Poll
                            • Submit Vote
                            • Post reward post
                            • Delete my own posts
                            • Delete my own threads
                            • Rate post

                            2000-2012 ASPPlayground.NET Forum Version 3.8
                            mbt shoes www.wileywilson.com