NT Batch for delete line

June 10, 2009 at 10:31:10
Specs: Windows XP Pro, 2GB
Can someone help me to create a bat file with which will be possible to delete all text in the text file that is placed between brackets.
------------------------------------------------------------------
Before deleting:
aaaaaaaaaaaa [ bbbbbbbbbbbb] cccccccccc
dddddddd [eeeeeeeee] fffffffffffffffff

After deleting:
aaaaaaaaaaaa cccccccccc
dddddddd fffffffffffffffff
------------------------------------------------------------------
Thanks in advance.


See More: NT Batch for delete line

Report •


#1
June 10, 2009 at 13:38:53
Not sure the script acts as you want, so try.

@echo off > output.txt
for /F "tokens=1,2,* delims=[]" %%i in (input.txt) do (
  echo.%%i%%k>> output.txt
)


Report •

#2
June 10, 2009 at 16:19:50
This is exactly what I asked.
Thank you very much on your help IVO.

Report •

#3
June 10, 2009 at 18:53:05
the batch version would not work if you have extra [] eg
aaaaaaaaaaaa [ bbbbbbbbbbbb] cccccccccc [ddddd ] eeeee
dddddddd [eeeeeeeee] fffffffffffffffff gggg [  hhhh ]

here's an alternative solution in Python that takes care of many [] pairs

import re,sys
file=sys.argv[1]
pat = re.compile("\[.*?\]",re.M|re.DOTALL)
data=open(file).read()
print pat.sub("",data)

output
C:\test>more file.txt
aaaaaaaaaaaa [ bbbbbbbbbbbb] cccccccccc [ddddd ] eeeee
dddddddd [eeeeeeeee] fffffffffffffffff gggg [  hhhh ]

C:\test>python test.py file.txt
aaaaaaaaaaaa  cccccccccc  eeeee
dddddddd  fffffffffffffffff gggg

redirect to new file using ">" as needed.

also, if you are interested, here's the executable tested on XP/win2k


Report •

Related Solutions

#4
June 11, 2009 at 01:34:10
**************
Thanks ghostdog.
**************
Pyton is superior regarding to NT batch files, but batch is
more favorable for us, those who do not know programming,
because it is simpler and straight away executive.

In this form it erases more brackets in the line,
only bat file should multiple times to run or make a loop.
--------------------------------------------------------------
@echo off > output.txt
if exist "input1.txt" (del /f /q "input1.txt")
for /F "tokens=1,2,* delims=[]" %%i in (input.txt) do (
echo.%%i%%k>> output.txt
)
if exist "input.txt" ren "input.txt" "input1.txt"
if exist "output.txt" ren "output.txt" "input.txt"
--------------------------------------------------------------
input.txt before:
aaaaaaaaaaaa [ bbbbbbbbbbbb] cccccccccckkkkkkkk[llllllllll] mmmm [nnnnnnn]
dddddddd [eeeeeeeee] fffffffffffffffff kkkkkkkk[llllllllll] mmmm [nnnnnnn]
gggggggg [hhhhhhhhh] jjjjjjjjjjjjjjj kkkkkkkk[llllllllll] mmmm [nnnnnnn]
kkkkkkkk[llllllllll] mmmm [nnnnnnn] kkkkkkkk[llllllllll] mmmm [nnnnnnn]

input.txt after 4 times running:
aaaaaaaaaaaa cccccccccckkkkkkkk mmmm
dddddddd fffffffffffffffff kkkkkkkk mmmm
gggggggg jjjjjjjjjjjjjjj kkkkkkkk mmmm
kkkkkkkk mmmm kkkkkkkk mmmm


Report •

#5
June 11, 2009 at 01:52:22
>> those who do not know programming,
because it is simpler and straight away executive.

how so? for those who do not know programming, nothing is simple (at first of course)

>>input.txt after 4 times running:

and if you have more square brackets, how many times do you think you have to run the script? manually??


Report •

#6
June 11, 2009 at 09:30:51
Thanks a lot gostdog.
You're one of the valuable members of
this forum who is always ready to help us.

Report •


Ask Question