Replace text in a text file (pre-installed environment)
Moderator: DosItHelp
Replace text in a text file (pre-installed environment)
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
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
Last edited by shanet on 05 Apr 2010 04:06, edited 1 time in total.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Replace text in a text file (pre-installed environment)
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)
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
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)
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
Re: Replace text in a text file (pre-installed environment)
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
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
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Replace text in a text file (pre-installed environment)
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)
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)
Great! Got it and it works. Thanks !k for the script!
Shanet
Shanet
Re: Replace text in a text file (pre-installed environment)
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
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)
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.
Shanet
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.
Shanet
Re: Replace text in a text file (pre-installed environment)
Is there any way to only make it look at line 13???
Shanet
Shanet
Re: Replace text in a text file (pre-installed environment)
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.