Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Replace String with Wildcards using DOS Batch file!  (Read 36267 times)

0 Members and 1 Guest are viewing this topic.

graymj

    Topic Starter


    Rookie

    Thanked: 1
    Replace String with Wildcards using DOS Batch file!
    « on: August 17, 2010, 05:09:22 PM »
    HI Yall  I know how to find and replace exact matches but how do you find
    and replace when part of the string changes?  Example:
    each line of the search file contains may or not have the string i'm looking for.  In the example, I only want to remove the PT at the end of the lines that start with GOTO AND END WITH PT & some numbers! Thanks 4 Help!

    FEDRAT/5000
    GOTO  / -476.19460,   -2.27892,  793.84030, -0.74, 0.014, 0.663 PT  5
    FEDRAT/5000
    GOTO  /  428.02960,  -19.31639,   -8.79631, -0.74, 0.014, 0.663 PT  6
    FEDRAT/500
    GOTO  /  446.72450,  -19.66865,  -25.39084, -0.74, 0.014, 0.663 PT  7
    GOTO  /  456.86450,  -25.60231,  -13.84143, -0.74, 0.014, 0.663 PT  8

    ghostdog74



      Specialist

      Thanked: 27
      Re: Replace String with Wildcards using DOS Batch file!
      « Reply #1 on: August 17, 2010, 06:17:11 PM »
      download sed for windows, then do this
      Code: [Select]
      sed -i.bak "/GOTO.*PT[ \t]*[0-9]$/s/PT//" file

      RoyBailey



        Rookie

        Replace String with Wildcards using DOS Batch file!
        « Reply #2 on: August 17, 2010, 06:44:11 PM »

        remove pt



        C:test>type  pt.txt
        FEDRAT/5000
        GOTO  / -476.19460,   -2.27892,  793.84030, -0.74, 0.014, 0.663 PT  5
        FEDRAT/5000
        GOTO  /  428.02960,  -19.31639,   -8.79631, -0.74, 0.014, 0.663 PT  6
        FEDRAT/500
        GOTO  /  446.72450,  -19.66865,  -25.39084, -0.74, 0.014, 0.663 PT  7
        GOTO  /  456.86450,  -25.60231,  -13.84143, -0.74, 0.014, 0.663 PT  8



        C:test>sed  */GOTO.*PT[ \\t]*[0-9]$/s/PT//* pt.txt

        Output:

        FEDRAT/5000
        GOTO  / -476.19460,   -2.27892,  793.84030, -0.74, 0.014, 0.663   5
        FEDRAT/5000
        GOTO  /  428.02960,  -19.31639,   -8.79631, -0.74, 0.014, 0.663   6
        FEDRAT/500
        GOTO  /  446.72450,  -19.66865,  -25.39084, -0.74, 0.014, 0.663   7
        GOTO  /  456.86450,  -25.60231,  -13.84143, -0.74, 0.014, 0.663   8

        C:test>

        * use double quote for * with sed above
        USA

        graymj

          Topic Starter


          Rookie

          Thanked: 1
          Re: Replace String with Wildcards using DOS Batch file!
          « Reply #3 on: August 17, 2010, 07:22:38 PM »
          Must be DOS only!  Thank for the  unix!  im converting unix to dos!
          FEDRAT/5000
          GOTO  / -476.19460,   -2.27892,  793.84030, -0.74, 0.014, 0.663 PT  5
          FEDRAT/5000
          GOTO  /  428.02960,  -19.31639,   -8.79631, -0.74, 0.014, 0.663 PT  6
          FEDRAT/500
          GOTO  /  446.72450,  -19.66865,  -25.39084, -0.74, 0.014, 0.663 PT  7
          GOTO  /  456.86450,  -25.60231,  -13.84143, -0.74, 0.014, 0.663 PT  8
           
          finish format below!

          FEDRAT/5000
          GOTO  / -476.19460,   -2.27892,  793.84030, -0.74, 0.014, 0.663
          FEDRAT/5000
          GOTO  /  428.02960,  -19.31639,   -8.79631, -0.74, 0.014, 0.663
          FEDRAT/500
          GOTO  /  446.72450,  -19.66865,  -25.39084, -0.74, 0.014, 0.663
          GOTO  /  456.86450,  -25.60231,  -13.84143, -0.74, 0.014, 0.663

          ghostdog74



            Specialist

            Thanked: 27
            Re: Replace String with Wildcards using DOS Batch file!
            « Reply #4 on: August 17, 2010, 08:34:10 PM »
            Must be DOS only!  Thank for the  unix!  im converting unix to dos!
            unix and dos are operating systems. when you are talking about tools, it doesn't matter. The only thing that matters is whether you are restricted to download any tools to use in complement with your batch script. With that said, good luck.  I hope the batch expert Roy Bailey will help you with that.

            RoyBailey



              Rookie

              Re: Replace String with Wildcards using DOS Batch file!
              « Reply #5 on: August 17, 2010, 08:59:19 PM »
              unix and dos are operating systems. when you are talking about tools, it doesn\'t matter. The only thing that matters is whether you are restricted to download any tools to use in complement with your batch script. With that said, good luck.  I hope the batch expert Roy Bailey will help you with that.

              I do not follow? Dos, Unix or Batch?

              I thought you used sed?

              How do we convert Unix to Dos?  Im confused.  I have no idea what is being discussed?
              Good Luck
              USA

              ghostdog74



                Specialist

                Thanked: 27
                Re: Replace String with Wildcards using DOS Batch file!
                « Reply #6 on: August 17, 2010, 09:38:59 PM »
                I do not follow? Dos, Unix or Batch?
                DOS is an operating system. Same with Unix.
                A DOS batch file is just a series of executables combined to perform a task. I thought you know that already

                Quote
                I thought you used sed?
                So ? Its just a tool. A tool that is written to work in both Windows and *nix platforms. It has nothing to do with operating systems.

                Quote
                How do we convert Unix to Dos?  Im confused.  I have no idea what is being discussed?
                Good Luck
                The OP obviously only thought that sed is a *nix tool that can't run on a Windows platform, which is untrue.  sed can be used on a windows platform as its an exe file just like any other.

                The only reason he can't use is if there are policies that restrict its usage ( or he just doesn't want to download anything ). So are you clear now what i am saying?




                ghostdog74



                  Specialist

                  Thanked: 27
                  Re: Replace String with Wildcards using DOS Batch file!
                  « Reply #7 on: August 17, 2010, 09:41:14 PM »
                  #duplicate#

                  graymj

                    Topic Starter


                    Rookie

                    Thanked: 1
                    Re: Replace String with Wildcards using DOS Batch file!
                    « Reply #8 on: August 22, 2010, 07:19:18 AM »
                    Hi I'm restricted from downloadloading  unix utilities! and the PT plus any char that follows must be removed!

                    Sidewinder



                      Guru

                      Thanked: 139
                    • Experience: Familiar
                    • OS: Windows 10
                    Re: Replace String with Wildcards using DOS Batch file!
                    « Reply #9 on: August 22, 2010, 11:15:08 AM »
                    Quote
                    In the example, I only want to remove the PT at the end of the lines that start with GOTO AND END WITH PT & some numbers!

                    If you can't download SED, you can always use VBScript and regular expressions. You can also use Powershell which came with your system, including the Integrated Scripting Environment (ISE). It might have to be activated.

                    This little piece of VBScript doggerel may help:

                    Code: [Select]
                    Const ForReading = 1
                    Const ForWriting = 2

                    Set fso = CreateObject("Scripting.FileSystemObject")
                    Set objRE = CreateObject("VBScript.RegExp")
                    objRE.Global     = True
                    objRE.IgnoreCase = False
                    objRE.Pattern    = "GOTO\s(.*?)\sPT\s[0-9]*"

                    Set inFile = fso.OpenTextFile("c:\temp\file.txt", ForReading)
                    Set outFile = fso.OpenTextFile("c:\temp\file.chg", ForWriting, True)

                    Do Until inFile.AtEndOfStream
                    strLine = inFile.ReadLine
                    Set colMatches = objRE.Execute(strLine)
                    If colMatches.Count > 0 Then
                    strLine = Replace(strLine, "PT", "")
                    End If
                    outFile.WriteLine strLine
                    Loop

                    Save the script with a vbs extension and run from the command prompt as cscript scriptname.vbs

                    Be sure to change the inFile and outFile datanames to match your situation.

                    Good luck.  8)
                    The true sign of intelligence is not knowledge but imagination.

                    -- Albert Einstein

                    graymj

                      Topic Starter


                      Rookie

                      Thanked: 1
                      Re: Replace String with Wildcards using DOS Batch file!
                      « Reply #10 on: August 22, 2010, 06:13:12 PM »
                      Thx for your help, my attempt was to keep the entire project in one format or another,  all batch files or all  VB script!  my current project is 98% command lines(batch files)and this was the last item left to tackle!

                      Sidewinder



                        Guru

                        Thanked: 139
                      • Experience: Familiar
                      • OS: Windows 10
                      Re: Replace String with Wildcards using DOS Batch file!
                      « Reply #11 on: August 23, 2010, 11:30:42 AM »
                      Correction:

                      Quote
                      This line in the VBScript solution: objRE.Pattern    = "GOTO\s(.*?)\sPT\s[0-9]*"
                      should be changed to: objRE.Pattern    = "^GOTO\s(.*?)\bPT\s{2}[0-9]{1,4}\b"
                                     
                      The original line will work, however lets just say the changed line is more accurate.

                      I'd hate to be responsible for your 2% shortfall of making your project all batch:

                      Code: [Select]
                      @echo off
                      setlocal enabledelayedexpansion
                      if exist c:\temp\regex.chg del c:\temp\regex.chg

                      for /f "tokens=* delims=" %%f in (c:\temp\regex.txt) do (
                        set strLine=%%f
                        set subLine=!strLine:~0,4!
                        if /i .!subLine! EQU .GOTO set strLine=!strLine:PT=!
                        echo !strLine! >> c:\temp\regex.chg
                      )

                      The batch solution is less verbose but also less generic. Be sure to change the file names for your environment.

                      Good luck. 8)
                      The true sign of intelligence is not knowledge but imagination.

                      -- Albert Einstein

                      graymj

                        Topic Starter


                        Rookie

                        Thanked: 1
                        Re: Replace String with Wildcards using DOS Batch file!
                        « Reply #12 on: August 24, 2010, 04:40:48 AM »
                        This works almost AND THANK 4 YOUR TIME!  but it only removes the PT I need the numbers that follows the PT removed too!

                        Must be DOS only!  Thank for the  unix!  im converting unix  scripts to dos batch files!
                        FEDRAT/5000
                        GOTO  / -476.19460,   -2.27892,  793.84030, -0.74, 0.014, 0.663 PT  5
                        FEDRAT/5000
                        GOTO  /  428.02960,  -19.31639,   -8.79631, -0.74, 0.014, 0.663 PT  6
                        FEDRAT/500
                        GOTO  /  446.72450,  -19.66865,  -25.39084, -0.74, 0.014, 0.663 PT  7
                        GOTO  /  456.86450,  -25.60231,  -13.84143, -0.74, 0.014, 0.663 PT  8
                         
                        Your Script

                        FEDRAT/5000
                        GOTO  / -476.19460,   -2.27892,  793.84030, -0.74, 0.014, 0.663   5
                        FEDRAT/5000
                        GOTO  /  428.02960,  -19.31639,   -8.79631, -0.74, 0.014, 0.663   6
                        FEDRAT/500
                        GOTO  /  446.72450,  -19.66865,  -25.39084, -0.74, 0.014, 0.663   7
                        GOTO  /  456.86450,  -25.60231,  -13.84143, -0.74, 0.014, 0.663   8

                        But should look like below!
                         
                        FEDRAT/5000
                        GOTO  / -476.19460,   -2.27892,  793.84030, -0.74, 0.014, 0.663   
                        FEDRAT/5000
                        GOTO  /  428.02960,  -19.31639,   -8.79631, -0.74, 0.014, 0.663   
                        FEDRAT/500
                        GOTO  /  446.72450,  -19.66865,  -25.39084, -0.74, 0.014, 0.663   
                        GOTO  /  456.86450,  -25.60231,  -13.84143, -0.74, 0.014, 0.663   


                        ghostdog74



                          Specialist

                          Thanked: 27
                          Re: Replace String with Wildcards using DOS Batch file!
                          « Reply #13 on: August 24, 2010, 05:20:24 AM »
                          Show some effort on your part. Its your project after all.
                          What references are you reading now regarding DOS batch scripting ? Or have you been reading up on it at all? Or are you just blatantly waiting for the solution to come to you? by the time someone showed you how to do it, you might already have found the answer on the net (or by reading up).


                          graymj

                            Topic Starter


                            Rookie

                            Thanked: 1
                            Re: Replace String with Wildcards using DOS Batch file!
                            « Reply #14 on: August 24, 2010, 05:47:50 AM »
                            Show some effort!!!!  You have no idea what your are talking about!  I have spent over 6 weeks reading books and researching several sites!  I work on it around the clock!  attempting to use the example below!  I just don't post all my efforts here!  I would have over 20 pages of crap!

                            If you decide not to help!  Then do so Keep your replies silents!  I don't need any negitive responces!  >:(  Thank you!

                            FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
                            [/C:string] [/G:file] [/D:dir list] [/A:color attributes]
                            [strings] [[drive:][path]filename[ ...]]

                            Regular expression quick reference:

                            . Wildcard: any character


                            * Repeat: zero or more occurrences of previous character or class
                            ^ Line position: beginning of line
                            $ Line position: end of line
                            [class] Character class: any one character in set
                            [^class] Inverse class: any one character not in set
                            [x-z] Range: any characters within the specified range
                            \x Escape: literal use of metacharacter x
                            \<xyz Word position: beginning of word
                            xyz\> Word position: end of word