Batch help to replace 1 line by number in a text file
Moderator: DosItHelp
Batch help to replace 1 line by number in a text file
Hi Guys....
Batch file needed to replace one line in original.txt file with different text and make a copy
of it before replacing the line.
Example would be if I have a text file like below called original.txt and want to replace the word
orange with chapter 10.000 <----there must be a space between chapter and the number
Most important the orange word might be different so I need the batch file to
replace line 3 in this case whatever line 3 says.
raspberry
zebra
orange
black
apple
What would be the complete batch text please?
Something like ?:
@echo off
ren original.txt copy-of-original.txt
copy copy-of-original.txt original.txt <-----dunno if this line is correct
replace line 3 in original.txt with chapter 10.000 <--- this is another line I have no idea how to do
pause
Thanks!
Val
Batch file needed to replace one line in original.txt file with different text and make a copy
of it before replacing the line.
Example would be if I have a text file like below called original.txt and want to replace the word
orange with chapter 10.000 <----there must be a space between chapter and the number
Most important the orange word might be different so I need the batch file to
replace line 3 in this case whatever line 3 says.
raspberry
zebra
orange
black
apple
What would be the complete batch text please?
Something like ?:
@echo off
ren original.txt copy-of-original.txt
copy copy-of-original.txt original.txt <-----dunno if this line is correct
replace line 3 in original.txt with chapter 10.000 <--- this is another line I have no idea how to do
pause
Thanks!
Val
Re: Batch help to replace 1 line by number in a text file
Try this code:
It uses a helper batch file called `repl.bat` from - https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.
The ^ ensures that orange starts at the beginning of the line. .* means include the rest of the line.
The replace string is a regular expression so some characters need special treatment but alphanumerics are fine.
It uses a helper batch file called `repl.bat` from - https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.
Code: Select all
@echo off
ren "original.txt" "copy-of-original.txt"
type "copy-of-original.txt" |repl "^orange.*" "chapter 10.000" >"original.txt"
The ^ ensures that orange starts at the beginning of the line. .* means include the rest of the line.
The replace string is a regular expression so some characters need special treatment but alphanumerics are fine.
Re: Batch help to replace 1 line by number in a text file
Hey Foxidrive,
I think he just wants to replace whatever is in line 3. So basically stream the file out and replace line 3 with whatever he wants.
I think he just wants to replace whatever is in line 3. So basically stream the file out and replace line 3 with whatever he wants.
Re: Batch help to replace 1 line by number in a text file
Yo foxidrive....
The code you showed me here didn't work right at all.
It made a copy of the original file called copy-of-original.txt
and deleted all the content inside the original.txt so I ended up
with a blank original.txt.....lol...
Wanna try again?
Please...
Thanks!
Val
The code you showed me here didn't work right at all.
It made a copy of the original file called copy-of-original.txt
and deleted all the content inside the original.txt so I ended up
with a blank original.txt.....lol...
Wanna try again?
Please...
Thanks!
Val
Re: Batch help to replace 1 line by number in a text file
Code: Select all
C:\> type original.txt
raspberry
zebra
orange
black
apple
C:\> type test.bat
@echo off
ren original.txt copy-of-original.txt
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" copy-of-original.txt') do (
if %%a equ 3 (
echo chapter 10.000
) else (
echo %%b
)
)) > original.txt
C:\> test
C:\> type original.txt
raspberry
zebra
chapter 10.000
black
apple
Re: Batch help to replace 1 line by number in a text file
val5662 wrote:Yo foxidrive....
The code you showed me here didn't work right at all.
It made a copy of the original file called copy-of-original.txt
and deleted all the content inside the original.txt so I ended up
with a blank original.txt.....lol...
Wanna try again?
Please...
Thanks!
Val
I can't help it if you don't tell us what you really want to do.
Using the info in your original.txt that you told us was in there, it works fine.
Try it.
Re: Batch help to replace 1 line by number in a text file
Sorry foxidrive.I tried your exact code and same result.Thanks for trying anyway.I appreciate it!
Aacinis code worked 100%.
@echo off
ren original.txt copy-of-original.txt
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" copy-of-original.txt') do (
if %%a equ 3 (
echo chapter 10.000
) else (
echo %%b
)
)) > original.txt
Thanks a bunch Aacini!
Val
Aacinis code worked 100%.
@echo off
ren original.txt copy-of-original.txt
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" copy-of-original.txt') do (
if %%a equ 3 (
echo chapter 10.000
) else (
echo %%b
)
)) > original.txt
Thanks a bunch Aacini!
Val
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Batch help to replace 1 line by number in a text file
vbscript solution.
run as
Code: Select all
Dim objFSO,objFile,line
Const ForReading=1, ForWriting=2
Set objFSO= CreateObject("Scripting.FileSystemObject")
Set objFileRead = objFSO.OpenTextFile("C:\original.txt", 1)
Set objFileWrite = objFSO.CreateTextFile("C:\copyoriginal.txt")
Do Until objFileRead.AtEndOfLine
objFileWrite.WriteLine(line)
If objFileRead.Line = 3 Then
objFileRead.SkipLine
Else
line = objFileRead.ReadLine
WScript.Echo line
End If
Loop
objFileRead.Close
objFileWrite.Close
run as
Code: Select all
c:\> cscript //nologo myscript.vbs
Re: Batch help to replace 1 line by number in a text file
val5662 wrote:Sorry foxidrive.I tried your exact code and same result.Thanks for trying anyway.I appreciate it!
Did you fail to read my post, and download the helper batch file?
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Batch help to replace 1 line by number in a text file
foxidrive wrote:Try this code:
It uses a helper batch file called `repl.bat` from - https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
wow, jscript. so unconventional.
Re: Batch help to replace 1 line by number in a text file
ghostmachine4 wrote:foxidrive wrote:Try this code:
It uses a helper batch file called `repl.bat` from - https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
wow, jscript. so unconventional.
You may smile
Both repl.bat and findrepl.bat are native solutions to much of what SED and GREP can do, respectively.
They both employ jscript and give us *robust* solutions, where batch code is long winded and
susceptible to many issues: such as poison characters, blank lines, foreign characters, EOL problems etc.
I've been into batch files for decades now and battled with code, purely for the fun of it, but when supplying
help for other people and when you have no idea what the source material is - these recently developed tools are fantastic.
Dave Benham (repl.bat) and Aacini (findrepl.bat) developed them.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Batch help to replace 1 line by number in a text file
foxidrive wrote:ghostmachine4 wrote:foxidrive wrote:Try this code:
It uses a helper batch file called `repl.bat` from - https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
wow, jscript. so unconventional.
You may smile
Both repl.bat and findrepl.bat are native solutions to much of what SED and GREP can do, respectively.
what i mean by "unconventional" is that usually the people will just think of vbscript first instead of jscript .
Re: Batch help to replace 1 line by number in a text file
foxidrive wrote:val5662 wrote:Sorry foxidrive.I tried your exact code and same result.Thanks for trying anyway.I appreciate it!
Did you fail to read my post, and download the helper batch file?
foxi, your code work in my window 7 32-bit
Re: Batch help to replace 1 line by number in a text file
bars143 wrote:val5662 wrote:Sorry foxidrive.I tried your exact code and same result.Thanks for trying anyway.I appreciate it!
foxi, your code work in my window 7 32-bit
Thanks. I think val5662 is a newbie and has to pick up some skills that we take for granted.
Re: Batch help to replace 1 line by number in a text file
foxidrive wrote:bars143 wrote:val5662 wrote:Sorry foxidrive.I tried your exact code and same result.Thanks for trying anyway.I appreciate it!
foxi, your code work in my window 7 32-bit
Thanks. I think val5662 is a newbie and has to pick up some skills that we take for granted.
i create new folder containing :
1.) your code in batch file
2.) repl.bat
3.) original.txt <--- this is the one missed by the author as i tried it to skip this and will result in blank content.