Saved Bookmarks
| 1. |
Solve : Replace String with Wildcards using DOS Batch file!? |
|
Answer» Quote from: 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!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. 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. 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! 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! Quote from: graymj on August 24, 2010, 08:56:41 AM I'm well versed in Pearl Pearl? For some reason I suspect you meant: Perl That would be correct! Thanks for the correction! 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. 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! THANKSSSSSSSSSSS!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!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. 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!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!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 LoopTo 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 )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. |
|