Page 1 of 1

Search & Replace - I'm getting crazy

Posted: 07 Apr 2010 09:02
by six1zero
Hi,
probably it's a stupid thing but I don't understand what is wrong.

I tried to use BatchSubstitute.bat, but doesn't work on my machine.

C:\>BatchSubstitute.bat Andrea AAA ReinsuranceValues.txt
find: invalid predicate `'
The process tried to write to a nonexistent pipe.

I tried to change the predicate ' with ´ or ` but the error is:

C:\>BatchSubstitute.bat Andrea AAA Values.txt
The system cannot find the file ┤"type Values.txt|find /n /v """┤.

C:\>BatchSubstitute.bat Andrea AAA Values.txt
The system cannot find the file `"type Values.txt|find /n /v """`.

Which is the secrects to use it?

Thanks in advance
Andrea

Re: Search & Replace - I'm getting crazy

Posted: 07 Apr 2010 10:18
by six1zero
without single quote '

C:\>BatchSubstitute.bat Andrea AAA Values.txt
"%line%"

C:\>

Re: Search & Replace - I'm getting crazy

Posted: 08 Apr 2010 04:27
by ghostmachine4
i recommend you to try out sed. download from : http://gnuwin32.sourceforge.net/packages/sed.htm

Code: Select all

c:\test> sed -i.bak "s/Andrea/AAA/g" file.txt


-i.bak is an option to save your file first before sed makes changes to your file.

Re: Search & Replace - I'm getting crazy

Posted: 08 Apr 2010 09:05
by six1zero
Thanks for your suggestion.

I resolve the problem removing the find after pipe, more slow but works.
Following the code to resolve my problem, now the batch file don't need the parameters and works automatically with multiple file in a particulary directory.

Thanks to dostips!

@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat
:: OldStr [in] - string to be replaced
:: NewStr [in] - string to replace with
:: File [in] - file to be parsed
:$source http://www.dostips.com

SET ELS_DRIVE=%ELSHOMEDIRECTORY%
SET SPOOLTV=%ELS_DRIVE%\SpoolTV
SET FILE_PREFIX=ReinsuranceValues
SET FILE_SUFFIX=.ps

echo %ELS_DRIVE%
%ELS_DRIVE%
cd %SPOOLTV%

REM -- OldStr [in] - string to be replaced
SET OldStr=P:

REM -- NewStr [in] - string to replace with
SET NewStr=

echo %FILE_PREFIX%*%FILE_SUFFIX%

for %%i in (%FILE_PREFIX%*%FILE_SUFFIX%) do CALL :MIO %%i

:MIO
SET myTempFile=%1
IF "%myTempFile%"=="" GOTO:EOF
echo.
echo %myTempFile%

REM -- Get first part of file name until "." (dot) and the second part after "." (dot)
for /f "tokens=1,2 delims=. " %%a in ("%myTempFile%") do set myNameFile=%%a&set myExtensionFile=%%b

REM -- Rename the original file (ex. FileName_orig.extension)
set myFile=%myNameFile%.%myExtensionFile%.orig
ren %myTempFile% %myFile%


if "%OldStr%"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims==" %%A in ('"type %myFile%"') do (
set "line=%%A"
if defined line (
call set "line=echo.%%line:%OldStr%=%NewStr%%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X>>%myTempFile%
) ELSE echo.
)

Re: Search & Replace - I'm getting crazy

Posted: 04 May 2010 16:56
by srikarmrao
If working on windows servers, you can use change.com pathofthefile %oldstr% %newstr%

change.com is a windows utilities tool. free for windows servers we can download from microsoft website.