Computer Hope

Microsoft => Microsoft DOS => Topic started by: graymj on August 17, 2010, 05:09:22 PM

Title: Replace String with Wildcards using DOS Batch file!
Post by: graymj 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
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: ghostdog74 on August 17, 2010, 06:17:11 PM
download sed for windows (http://gnuwin32.sourceforge.net/packages/sed.htm), then do this
Code: [Select]
sed -i.bak "/GOTO.*PT[ \t]*[0-9]$/s/PT//" file
Title: Replace String with Wildcards using DOS Batch file!
Post by: RoyBailey 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
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: graymj 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
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: ghostdog74 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.
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: RoyBailey 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
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: ghostdog74 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?



Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: ghostdog74 on August 17, 2010, 09:41:14 PM
#duplicate#
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: graymj 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!
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: Sidewinder 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)
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: graymj 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!
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: Sidewinder 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)
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: graymj 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   

Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: ghostdog74 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).

Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: graymj 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


Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: ghostdog74 on August 24, 2010, 06:03:38 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!
sidewinder has shown you how to do it with batch, although not to your requirement. But i am expecting you follow his guidance on that piece of snippet and work on it. That's the effort i am talking about. I am not expecting you to post all your already done scripts here.  So i am saying,  are you really waiting for him to solve your own (project/homework) problem?

Quote
If you decide not to help!  Then do so Keep your replies silents!  I don't need any negitive responces!  >:(  Thank you!
we are not here to do your work/project for you. As you already saw, I have helped you a lot. I have shown you ways you could do it with ease, and others have shown you native ways to do it with batch and vbscript. BUT the problem is caused by you yourself. Only DOS is allowed ? Typical project/school homework restriction , isn't it ?

If the DOS you mentioned is really the MSDOS 6.22 , it most probably can't be done in a pure DOS, except you have to use some extra tools. If it can be done, it would probably be obscure and arcane. Either way, you are on my blacklist of people not to help from now on.
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: Sidewinder on August 24, 2010, 08:12:29 AM
I must have misinterpreted your original post about the numerics after the PT to be deleted too. In any case, that created a real problem when trying to create a batch file to do just that. Seems those special characters in the data file cause the NT interpreter to choke.

By the way, the VBScript I posted only removed the PT not the following numerics. This little ditty fixes that problem:

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

Set fso = CreateObject("Scripting.FileSystemObject")
Set objRE = CreateObject("VBScript.RegExp")
objRE.Global     = True
objRE.IgnoreCase = False

Set inFile = fso.OpenTextFile("d:\wfc\sniplib\WSH-RegEx-putSearchReplace.txt", ForReading)
Set outFile = fso.OpenTextFile("c:\temp\Regex.chg", ForWriting, True)

Do Until inFile.AtEndOfStream
strLine = inFile.ReadLine
objRE.Pattern = "^GOTO\s(.*?)\bPT\s{2}[0-9]{1,}\b"
Set colMatches = objRE.Execute(strLine)
If colMatches.Count > 0 Then 
objRE.Pattern = "PT\s{2}"               'remove PT leave remaining digit(s)
  objRE.Pattern = "PT\s{2}[0-9]{1,}\b"   'remove PT and remaining digit(s) 
strLine = objRE.Replace(strLine, "")
End If
outFile.WriteLine strLine
Loop

Considering you have Win7 and all those tools available, I would have thought a batch solution would be your last choice.

Good luck.  8)
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: graymj on August 24, 2010, 08:43:22 AM
Thank You Sidewinder!  I got your vb to work as well!  Yes there was issues
understanding my orginal request!  I read everything I could find on the subject! But there seem to be no way to do this in DOS! Learn alot from U
and your example!  Thank many time more! :)
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: graymj on August 24, 2010, 08:56:41 AM
Sorry ghostdog74 you feel that way! And as Sidewinder has come to undrstand that my orginal requirements were missunderstoud!  Which I attempted to point out with several examples!  I want no one to do my work!  I had a programming issue and tried to throw it out here to help myself and others learning batch programming on DOS, which I'm more
than sure this solution will!  I'm well versed in Pearl & writing UNIX Scripts,
both would only require one liners to solve this issue!  But there was a company standard for whatever reason to only use DOS!  which I'm attempting to do!   Best Wishes and Thanks for your help in the pass as well!
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: BC_Programmer on August 24, 2010, 09:38:11 AM
  I'm well versed in Pearl

Pearl? (http://en.wikipedia.org/wiki/PEARL_%28programming_language%29)

For some reason I suspect you meant:

Perl (http://en.wikipedia.org/wiki/Perl)

Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: graymj on August 24, 2010, 09:50:56 AM
That would be correct!  Thanks for the correction! ;)
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: Sidewinder on August 24, 2010, 10:00:50 AM
I hate unfinished business, so I came up with this monstrosity. I apologize if it looks something like Curly the Neanderthal would mark on the cave wall. I gotta learn to stop over thinking things.

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 call :getLoc
  echo !strLine! >> c:\temp\regex.chg
)
goto :eof

:getLoc
  for /l %%i in (0, 1, 67) do (
    call set strChunk=%%strLine:~%%i,2%%
    if .!strChunk! EQU .PT call set strLine=%%strLine:~0,%%i%% & goto :eof

  )
  goto :eof

Change the file paths as appropriate. If the position of PT changes you may need to change the 67 value in the for /l statement to increase the search range.

Good luck. 8)
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: graymj on August 24, 2010, 12:44:00 PM
WOW!  HA HA!  How did you come of with this?  I can't wait to try it!

the logic seem wild!  I'm trying to break it down now! 8)  THANKSSSSSSSSSSS!
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: graymj on August 24, 2010, 03:10:58 PM
HI Sidewinder It Works Great!  Just having a few problems intergrateing into my code!  but thats a small problem!  also developeing a method to
Identify which column the PT start and pass that value to your routine!
You're the BEST!  THANKS AGAIN & AGAIN!
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: Sidewinder on August 24, 2010, 03:50:22 PM
There is no need to send the starting position of PT to the routine. The whole point was to make the code generic. The code found PT in the data file you posted at offset 64 in all the records that contained it.

The :getLoc routine processes records that have GOTO in the first four bytes. By starting at offset 0 (record position 1), it increases the record position by 1, reading 2 byte chunks of the record until PT is found  or offset 67 is reached, whichever comes first. The 67 was arbitrary. Once the offset of PT is found (stored in the %%i token), the code uses truncation to eliminate the high order bytes. The logic is fairly simple, it was the batch notation that was challenging.

The FOR /L %variable IN (start,step,end) DO statement uses the "end" parameter as the indicator when to stop the loop. In this case it represents the highest offset to check before quitting the loop. You can exceed the record length without the code throwing an error. Ensure the "end" parameter is large enough to include the entire record without being so large as to needlessly waste CPU cycles.

The code can probably be tweaked for more efficiency. For instance, the search for PT could start at offset 4; we already know offsets 0-3 contains GOTO

Hope this helps.  8)
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: graymj on August 24, 2010, 07:56:47 PM
Thanks!  I was busy changeing the 67 thinking it was a start position!  but after checking out other sites realized it was like an range!  Your explanation was great!  This is avery useful piece of code!  Thx Again for the info!
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: drocks on December 29, 2010, 12:05:52 PM
Is there a way to modify the VBS code example (posted by Sidewinder - thanks by the way) to replace the expression found, ie, find /.*$ (evrything from the slash to enf of line) and replace it with a blank?  I am new to vbs, and am not sure what the syntax is to substitute something for the found expression -

In advance, thanks!
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: drocks on December 30, 2010, 09:35:55 AM
Scratch my previous post, I did not see the ealier post from Sidewinder with an example of exactly what I needed (once again, thanks very much!).  I modified it slightly for what I needed (below), and it works beautufully.
Thanks Sidewinder!!

Const ForReading = 1
Const ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")
Set objRE = CreateObject("VBScript.RegExp")
objRE.Global     = True
objRE.IgnoreCase = True

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

Do Until inFile.AtEndOfStream
   strLine = inFile.ReadLine
   objRE.Pattern = "=/.[^/]*"
   Set colMatches = objRE.Execute(strLine)
   If colMatches.Count > 0 Then 
       objRE.Pattern = "=/.[^/]*"   'remove everyting from = up to but not including next /, replace with =
      strLine = objRE.Replace(strLine, "=")
   End If
   outFile.WriteLine strLine
Loop
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: kmwittko on March 29, 2011, 06:43:55 PM
To replace all occurrences of one string in a file by another string, there is even a simpler solution:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%f in (%1) do (
  set strLine=%%f
  set strLine=!strLine:   = !
::                  tab^   ^space
  echo !strLine! >> %1
)
Title: Re: Replace String with Wildcards using DOS Batch file!
Post by: kmwittko on March 30, 2011, 02:14:14 PM
Update:

1) The output file, of course should be #2 :-(
2) Only exclamation marks, the text between two exclamation marks, and empty lines that are lost. All the other known problem characters (both outside and inside of "..." and "%...%") are retained.