Page 1 of 1
Replace text in a text file (pre-installed environment)
Posted: 04 Apr 2010 21:47
by shanet
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
Shanet
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!
Shanet
Re: Replace text in a text file (pre-installed environment)
Posted: 05 Apr 2010 03:00
by ghostmachine4
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
Re: Replace text in a text file (pre-installed environment)
Posted: 06 Apr 2010 02:23
by shanet
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
Shanet
Re: Replace text in a text file (pre-installed environment)
Posted: 06 Apr 2010 02:45
by !k
shanetCode: 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
Re: Replace text in a text file (pre-installed environment)
Posted: 06 Apr 2010 03:51
by shanet
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
Shanet
Re: Replace text in a text file (pre-installed environment)
Posted: 06 Apr 2010 04:57
by ghostmachine4
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
Shanet
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.
Re: Replace text in a text file (pre-installed environment)
Posted: 06 Apr 2010 06:03
by shanet
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,
Shanet
Re: Replace text in a text file (pre-installed environment)
Posted: 06 Apr 2010 10:55
by shanet
Great! Got it and it works. Thanks !k for the script!
Shanet
Re: Replace text in a text file (pre-installed environment)
Posted: 06 Apr 2010 11:49
by shanet
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
my bad
can any1 help plz?
Thanks,
Shanet
Re: Replace text in a text file (pre-installed environment)
Posted: 06 Apr 2010 12:18
by shanet
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
orLine19
All I want to do is edit 1 bit of this without harming the rest. Is it even possible?
Thankyou all for your time.
Shanet
Re: Replace text in a text file (pre-installed environment)
Posted: 06 Apr 2010 20:42
by shanet
Is there any way to only make it look at line 13???
Shanet
Re: Replace text in a text file (pre-installed environment)
Posted: 07 Apr 2010 03:27
by !k
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.