Batch help to replace 1 line by number in a text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
val5662
Posts: 34
Joined: 18 Dec 2013 09:48

Batch help to replace 1 line by number in a text file

#1 Post by val5662 » 18 Dec 2013 12:00

Hi Guys.... :D
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

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

Re: Batch help to replace 1 line by number in a text file

#2 Post by foxidrive » 18 Dec 2013 18:20

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.

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.

Squashman
Expert
Posts: 4195
Joined: 23 Dec 2011 13:59

Re: Batch help to replace 1 line by number in a text file

#3 Post by Squashman » 18 Dec 2013 18:59

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.

val5662
Posts: 34
Joined: 18 Dec 2013 09:48

Re: Batch help to replace 1 line by number in a text file

#4 Post by val5662 » 18 Dec 2013 19:08

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

Aacini
Expert
Posts: 1669
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: Batch help to replace 1 line by number in a text file

#5 Post by Aacini » 18 Dec 2013 19:18

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

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

Re: Batch help to replace 1 line by number in a text file

#6 Post by foxidrive » 18 Dec 2013 19:29

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.

val5662
Posts: 34
Joined: 18 Dec 2013 09:48

Re: Batch help to replace 1 line by number in a text file

#7 Post by val5662 » 18 Dec 2013 21:38

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

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

Re: Batch help to replace 1 line by number in a text file

#8 Post by ghostmachine4 » 18 Dec 2013 22:11

vbscript solution.

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

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

Re: Batch help to replace 1 line by number in a text file

#9 Post by foxidrive » 18 Dec 2013 22:42

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?

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

Re: Batch help to replace 1 line by number in a text file

#10 Post by ghostmachine4 » 18 Dec 2013 23:10

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. :D

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

Re: Batch help to replace 1 line by number in a text file

#11 Post by foxidrive » 18 Dec 2013 23:48

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. :D


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.

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

Re: Batch help to replace 1 line by number in a text file

#12 Post by ghostmachine4 » 19 Dec 2013 00:06

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. :D


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 :) .

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: Batch help to replace 1 line by number in a text file

#13 Post by bars143 » 19 Dec 2013 02:51

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 :)

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

Re: Batch help to replace 1 line by number in a text file

#14 Post by foxidrive » 19 Dec 2013 03:06

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.

bars143
Posts: 87
Joined: 01 Sep 2013 20:47

Re: Batch help to replace 1 line by number in a text file

#15 Post by bars143 » 19 Dec 2013 03:22

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.

Post Reply