Replace text in a text file (pre-installed environment)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
shanet
Posts: 11
Joined: 04 Apr 2010 21:22

Replace text in a text file (pre-installed environment)

#1 Post by shanet » 04 Apr 2010 21:47

Hey everyone.

I have been doing a lot of browsing, and have not been able to find what I want to find.

What I want to do is have a batch on my dekstop that will edit a file with a different directory eg.
'C:\Program Files\example\text_file.txt'.

The above directory is an example of where the text file is.
If the text file contained the text:

"Negative1"
"Negative2"
"Positive1"
"Positive2"
"Negative3"
"Positive3"

I want to change the "Positive" in "Positive2" to a "Negative", inside the text file by running this batch file, however the command can't conflict with other text (eg menus, other commands, and a lot of echoing).

I have used the FIND command to find it, but It doesn't have the option to edit/change.

I also need the option to do it silently.

Can anyone please help, all help is greatly appreciated.

Thanking you all in advance

:twisted: Shanet :twisted:



P.S. I want to be able to give this to others who will have the file so I dont want to have to use third party software or anything. Thanks for the responses!

:twisted: Shanet :twisted:
Last edited by shanet on 05 Apr 2010 04:06, edited 1 time in total.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Replace text in a text file (pre-installed environment)

#2 Post by ghostmachine4 » 05 Apr 2010 03:00

download sed for windows at gnuwin32.sourceforge.net/packages/sed.htm, then put this in your batch file

Code: Select all

sed -i.bak "s/positive2/negative2/" file

shanet
Posts: 11
Joined: 04 Apr 2010 21:22

Re: Replace text in a text file (pre-installed environment)

#3 Post by shanet » 06 Apr 2010 02:23

Many Thanks to Ghostmachine4.

I am wondering - I want to convert it to an exe file with bat2exe - If I include the 'sed' file, will it work on other computers?
eg
If I email this file to my friend to help him with a problem, will it run or will he need the 'sed' file.
if he needs the 'sed' file, will he need to install it or does it run as a stand alone, in which case I can ind it to this file?

Many Thanks
:twisted: Shanet :twisted:

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Replace text in a text file (pre-installed environment)

#4 Post by !k » 06 Apr 2010 02:45

shanet

Code: Select all

@echo off
set "orig=Positive2"
set "repl=Negative2"

move "text_file.txt" "text_file.bak"
for /f "usebackq delims=" %%a in ("text_file.bak") do call :r "%%~a"
exit /b

:r
set "l=%~1"
if "%l%"=="%orig%" set "l=%repl%"
echo "%l%">>"text_file.txt"
goto :eof

shanet
Posts: 11
Joined: 04 Apr 2010 21:22

Re: Replace text in a text file (pre-installed environment)

#5 Post by shanet » 06 Apr 2010 03:51

Thanks !k.

Im sorry for my thickness, but could you please explain this a bit more in depthly? I dont understand what you are doing?

Thanks
:twisted: Shanet :twisted:

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Replace text in a text file (pre-installed environment)

#6 Post by ghostmachine4 » 06 Apr 2010 04:57

shanet wrote:Many Thanks to Ghostmachine4.

I am wondering - I want to convert it to an exe file with bat2exe - If I include the 'sed' file, will it work on other computers?
eg
If I email this file to my friend to help him with a problem, will it run or will he need the 'sed' file.
if he needs the 'sed' file, will he need to install it or does it run as a stand alone, in which case I can ind it to this file?

Many Thanks
:twisted: Shanet :twisted:

there's no need to convert to exe. sed.exe is just one executable, so you can put in thumb drive , or just zip together with your batch and email your friend.

shanet
Posts: 11
Joined: 04 Apr 2010 21:22

Re: Replace text in a text file (pre-installed environment)

#7 Post by shanet » 06 Apr 2010 06:03

there's no need to convert to exe. sed.exe is just one executable, so you can put in thumb drive , or just zip together with your batch and email your friend.


Again, thanks to Ghostmachine4 for your help, however, I would like to keep it to one file. I will try the answer posted by !k, if that doesnt work, I will get in touch.

Thanks for your time,

:twisted: Shanet :twisted:

shanet
Posts: 11
Joined: 04 Apr 2010 21:22

Re: Replace text in a text file (pre-installed environment)

#8 Post by shanet » 06 Apr 2010 10:55

Great! Got it and it works. Thanks !k for the script!

:twisted: Shanet :twisted:

shanet
Posts: 11
Joined: 04 Apr 2010 21:22

Re: Replace text in a text file (pre-installed environment)

#9 Post by shanet » 06 Apr 2010 11:49

All good, but I actually have it as follows:

Negative
Negative
Positive
Positive
Negative
Positive

I put the numbers there to make it easier... sorry
:oops:

my bad

can any1 help plz?

Thanks,

:twisted: Shanet :twisted:

shanet
Posts: 11
Joined: 04 Apr 2010 21:22

Re: Replace text in a text file (pre-installed environment)

#10 Post by shanet » 06 Apr 2010 12:18

I used this and it worked ok.

However, I have all this information that it destroys.


Lets get to another example, as all this information is a bit private (sorry for being really rude to u guys)

This file starts off being:

Line1
Line2
Line3
Line4
Line5
Line6
Line7
Line8

Line9
Line10
Line11

Line12
Line13
Line14

Line15
Line16
Line17

line18

Line19

etc...

I want Line 13 to change to say '13th line' (or whatever), but I want all the other information left alone. However with the code !k has iven me, it ends up as either of the following:

Line1

or


Line19

All I want to do is edit 1 bit of this without harming the rest. Is it even possible?

Thankyou all for your time.

:twisted: Shanet :twisted:

shanet
Posts: 11
Joined: 04 Apr 2010 21:22

Re: Replace text in a text file (pre-installed environment)

#11 Post by shanet » 06 Apr 2010 20:42

Is there any way to only make it look at line 13???


:twisted: Shanet :twisted:

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Replace text in a text file (pre-installed environment)

#12 Post by !k » 07 Apr 2010 03:27

Code: Select all

@echo off
set "num=13"
set "repl=13th line"

set "cnt=1"
move "text_file.txt" "text_file.bak"
for /f "usebackq delims=" %%a in ("text_file.bak") do call :r "%%~a"
exit /b

:r
set "l=%~1"
if "%cnt%"=="%num%" set "l=%repl%"
echo %l%>>"text_file.txt"
set /a cnt=%cnt%+1
goto :eof


Important!
Empty lines are ignored and not written to the output.
If the text includes special characters, for example &|()<>, need to take action on their screening.

Post Reply