Search and replace string inside text file - new-line fix

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Search and replace string inside text file - new-line fix

#1 Post by BoQsc » 06 May 2015 11:28

Hey everybody, i need some help again.
I have this script:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=test.txt
set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=bath
set REPLACETEXT=hello

for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!

echo !modified! >> %OUTTEXTFILE%
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%


Everything is nice and clear with it, but i'm thinking if there is way to modify script, so that in the output file the new-lines (those that are made using Enter button) won't be deleted?


foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Search and replace string inside text file - new-line fi

#3 Post by foxidrive » 06 May 2015 21:01

I'm not sure what he means, squashman.

BoQsc, can you please describe it a little more?

EDITED again: Ahh, you want to preserve the blank lines.

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Search and replace string inside text file - new-line fi

#4 Post by BoQsc » 07 May 2015 04:24

foxidrive wrote:I'm not sure what he means, squashman.

BoQsc, can you please describe it a little more?

EDITED again: Ahh, you want to preserve the blank lines.

Exactly, thanks for the comment good guy foxidrive, that corrects me. :)

Squashman, it seems that one symbol (^) is added after i parse the text file.
Is there any way to make it happen to disappear from output?

This is original file:

Code: Select all

asdkasldjlkasdklasdplease        
asmdasmdlplease  please       
 
      (for /f "delims=" %%i in ('findstr /n "" "%textfile%"') do (       


This is text file after parse:

Code: Select all

asdkasldjlkasdklasdhello       
asmdasmdlhello  hello     

      (for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (     


Error is in this line:

(for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Search and replace string inside text file - new-line fi

#5 Post by foxidrive » 07 May 2015 04:53

It works ok here. Show your code and explain your problem again.

I edited it to include a blank line as well.

Code: Select all

d:\abc>a.bat
1:asdkasldjlkasdklasdhello
2:
3:asmdasmdlhello  hello
d:\abc>type a.bat
@echo off

set "textfile=123.txt"
(
echo asdkasldjlkasdklasdhello
echo(
echo asmdasmdlhello  hello
)>"%textfile%"

(for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (      echo %%i ) )

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Search and replace string inside text file - new-line fi

#6 Post by BoQsc » 07 May 2015 07:58

Here is script that i use now:

Code: Select all

@echo off
:substitute OldStr NewStr File -- substitutes a string in a text file
::                             -- OldStr [in] - string to be replaced
::                             -- NewStr [in] - string to replace with
::                             -- File   [in] - file to be parsed
:$created 20060101 :$changed 20101122 :$categories FileManipulation
:$source http://www.dostips.com
SETLOCAL DISABLEDELAYEDEXPANSION
del output.txt

set "OldStr=baty"
set "NewStr=monty"
set filetoparse=%0
for /f "tokens=1,* delims=]" %%A in ('"type %filetoparse%|find /n /v """') do (
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%OldStr%=%NewStr%%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X >> output.txt
    ) ELSE echo.  >> output.txt

)
::baty
EXIT /b


it must scan batch file and replace ::baty with ::monty

The output.txt looks like this:

Code: Select all

@echo off 
:substitute OldStr NewStr File -- substitutes a string in a text file
::                             -- OldStr [in] - string to be replaced
::                             -- NewStr [in] - string to replace with
::                             -- File   [in] - file to be parsed
:$created 20060101 :$changed 20101122 :$categories FileManipulation
:$source http://www.dostips.com
SETLOCAL DISABLEDELAYEDEXPANSION
del output.txt
 
set "OldStr=monty"
set "NewStr=monty"
set filetoparse=%0
    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:%OldStr%=%NewStr%%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X >> output.txt
    ) ELSE echo.  >> output.txt
 
)
::monty
EXIT /b


whole line
for /f "tokens=1,* delims=]" %%A in ('"type %filetoparse%|find /n /v """') do (
has gone...

also, how to make that OldStr=baty will stay untouched while ::baty changing to ::monty?

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Search and replace string inside text file - new-line fi

#7 Post by BoQsc » 14 May 2015 08:53

foxidrive wrote:It works ok here. Show your code and explain your problem again.

I edited it to include a blank line as well.

Code: Select all

d:\abc>a.bat
1:asdkasldjlkasdklasdhello
2:
3:asmdasmdlhello  hello
d:\abc>type a.bat
@echo off

set "textfile=123.txt"
(
echo asdkasldjlkasdklasdhello
echo(
echo asmdasmdlhello  hello
)>"%textfile%"

(for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (      echo %%i ) )


The echo method is not what i'm searching for. I can't add echo to each line, cause it brokes some things.
Anyone have any idea on how to remove these line numbers?

Original:

Code: Select all

@echo off
asdkasldjlkasdklasdhello   

asmdasmdlhello  hello     

   (for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do test
    
     pause


Output:

Code: Select all

1:@echo off
2:asdkasldjlkasdklasdtest   
3:
4:asmdasmdltest  test     
5:
6:   (for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do test
7:    
8:     pause




The code i use is here:

Code: Select all

@echo off &setlocal
set "search=hello"
set "replace=test"
set "textfile=batch_code.bat"
set "newfile=new.bat"

(for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%newfile%"
type "%newfile%"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Search and replace string inside text file - new-line fi

#8 Post by foxidrive » 16 May 2015 02:29

BoQsc wrote:The echo method is not what i'm searching for. I can't add echo to each line, cause it brokes some things.
Anyone have any idea on how to remove these line numbers?


It was only to demonstrate that the syntax in the FOR loop works fine,
and you had removed the tokens part - and I used what you had posted.

If you don't show the real input and output text, or explain it well, it's hard to give you code that will work.

See here: viewtopic.php?f=3&t=6108

Post Reply