1.

Solve : Batch File to Remove a bracket and delete the line after the bracket.?

Answer»

Hi ,,

I am newbiew to the batch script. I want a batch file to delete the first bracket[( ] in a line and delete the bracket followed by a semi colon like this ); upto End of Line.


Example:

The text will be like this in a file test.txt.

#define VERSION_MAJOR_NUMBER 3 (version number (3333) it ); // declaration.


Now, I want the output to be in a same file or in a DIFF name like mentioned below.

#define VERSION_MAJOR_NUMBER 3 version number (3333) it





Please explain me step by step to achieve the output. I would make it run through the line one character at a time, and have the character that it finds change it's context...
This code has been tested and works for what you want
Code: [Select]set string=#define VERSION_MAJOR_NUMBER 3 (version number (3333) it ); // declaration.
set out=out.txt
set in=test.txt

for /f "tokens=* usebackq" %%f in ("%in%") do (set string=%%f
call :main)
goto :eof

:main
set mode=lead
:loop
set char=%string:~0,1%
set string=%string:~1%
call :route[%mode%]
if not "%string%"=="" if not "%mode%"=="end" goto :loop
echo %secstring%>>"%out%"
goto :eof

:route[lead]
if "%char%"=="(" (set mode=mid
goto :eof)
set secstring=%secstring%%char%
goto :eof

:route[mid]
if "%char%"==")" (set mode=testclose
goto :eof)
set secstring=%secstring%%char%
goto :eof

:route[testclose]
if "%char%"==";" (set mode=end
goto :eof)
set secstring=%secstring%)%char%
set mode=mid
goto :eof



There's the code, if you specifically want an explanation, then I can explain how it works.

[EDIT] I added the part that takes the INPUT from test.txt... misread the OP originally.
you can use sed.exe. downlaod from here

Code: [Select]@echo off
For /f "tokens=*" %%a In ('type test.txt ^|sed "s/(//" ^| sed "s/);.*//"') do (
echo %%a
)

Rem sed "s/(//" remove the first (
Rem sed "s/);.*// remove last bracket followed by ; and everything else.

Code: [Select]C:\>test.bat
#define VERSION_MAJOR_NUMBER 3 version number (3333) it
Hi Briandams,



Thanks for your valuable REPLY on this forum.
I tried it. but its not working for me. pls expalin the steps to try this. because i havent try like this before. Pls expalin step by step



Discussion

No Comment Found