Batch: replace specific lines in a txt file

September 23, 2010 at 08:05:58
Specs: Windows XP
Hello, I am new to Batch scripting and am having trouble doing the following.
I would like to have a batch script that scans a set of batch files and makes a set of replacements to the text data. The replacements involve: commenting out some known lines (using ::), replacing a set of 4 lines another set of 7 lines.
Looking forward to your help!

See More: Batch: replace specific lines in a txt file

Report •

#1
September 23, 2010 at 18:36:17
See my sig for Gawk. Its a good file processing tool able to do what you ask.

GNU win32 packages | Gawk


Report •

#2
September 24, 2010 at 06:48:53
I'm not with you on the 4 & 7 lines. But to REM out:

==========================================

@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in (myfile) do (
if "%%a"=="some text" (
  >> newfile echo.::%%a
  ) else (
  >> newfile echo.%%a
)
)

goto :eof

::===========================================


=====================================
Helping others achieve escape felicity

M2


Report •

#3
September 26, 2010 at 12:06:36
@ ghostdog: Thanks but I am looking to write a simple batch file for my task, not use tools.

@Mechanix2Go: Your script seems to search for lines with a certain string. For my case, I need to comment out certain lines (say lines 5, 10, 11 etc.). And about the 4&7 lines, I meant that I need to find and replace a block of 4 lines of text with a set of 7 lines. How would you achieve these?


Report •

Related Solutions

#4
September 26, 2010 at 19:48:36
>> to write a simple batch file for my task,

your batch will not be "simple" if you don't use the right choice of tools!.

GNU win32 packages | Gawk


Report •

#5
September 27, 2010 at 17:38:29
Ny ISP has been wonky. Here's the first part, REM out by line #. I'll get to the 4&7 bit later. This should work UNLESS the file has blank lines.
========================================
@echo off > newfile & setLocal enableDELAYedeXpansion

set N=
for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
set S=%%a
  for %%i in (5 7 8) do if !N! equ %%i set S=::!S!
>> newfile echo.!S!
)


=====================================
Helping others achieve escape felicity

M2


Report •

#6
September 28, 2010 at 03:59:52
Cool, it works!
But my txt file has blank lines unfortunately :(


Report •

#7
September 28, 2010 at 06:38:39
@echo off > newfile & setLocal enableDELAYedeXpansion

set N=
for /f "tokens=1* delims=[] " %%a in ('find /v /n "" ^< myfile') do (
set /a N+=1
set S=%%b
  for %%i in (5 7 8) do if !N! equ %%i set S=::!S!
>> newfile echo.!S!
)


=====================================
Helping others achieve escape felicity

M2


Report •

#8
September 28, 2010 at 07:55:37
...had meanwhile figured out that its fine if I use the earlier version and lose all the blank lines.

Currently breaking my head over the 4&7 puzzle. Another way to do it is that I can comment out the 4 lines not needed. That leaves me with the problem of inserting 3 new lines below them, could you help me out with that?


Report •

#9
September 28, 2010 at 08:31:02
correction to earlier comment: .....problem of inserting 7 new lines....

Report •

#10
September 28, 2010 at 08:47:55
If your 4 lines to replace are in rows 5 - 8 and the 7 newlines are in newseven.txt:
==============================================
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^< myfile') do (
if %%a leq 4 >> newfile echo.%%b
if %%a equ 4 >> newfile type newseven.txt
if %%a geq 9 >> newfile echo.%%b
)


=====================================
Helping others achieve escape felicity

M2


Report •

#11
September 29, 2010 at 08:39:20
Works! Thanks a lot M2 :)

By the way, is it possible to call a batch file with wildcard in the file name?
Am not able to get this working...


Report •

#12
September 29, 2010 at 08:49:31
Do you mean that there are 2 bats, b1.bat and b2.bat and you hope to call them with:

call b?

NO


=====================================
Helping others achieve escape felicity

M2


Report •

Ask Question