How to remove specific text from a protocol file
Moderator: DosItHelp
-
- Posts: 6
- Joined: 21 Sep 2009 15:56
How to remove specific text from a protocol file
Hey all,
I have a protocol-kind-of file which contains some entries like these:
WORLD =
(DESCRIPTION =
(DESC =
(ADDRESS = (PROTOCOL = IPC))
(ADDRESS = (PROTOCOL = TCP))
)
)
SERVER_LIST =
(LIST =
(DESCRIPTION =
(NAME = MyFolder)
(PATH = C:\bach\path\home_1)
(folder_name = MyFolder)
(Other line)
)
(DESCRIPTION =
(NAME = YourFolder)
(PATH = C:\bach\path\home_2)
(folder_name = MyFolder)
)
(DESCRIPTION =
(NAME = TheirFolder)
(PATH = C:\bach\path\home_3)
)
)
I want to copy all the content of this file but:
(DESCRIPTION =
(NAME = YourFolder)
(PATH = C:\bach\path\home_2)
(folder_name = MyFolder)
)
to a result file.
I'm thinking on this pseudocode:
1. Get line number containing "TheirFolder" entry -> %L1%
2. Count open (%op%) and close (%cp%) parenthesis from %L1%-1 until %op% - %cp% equals ZERO -> %L2%
3. Copy all that it is before %L1% - 1 to ResultFile.txt
4. Append all that it is after %L2% to ResultFile.txt
This have to be done merely using Windows batch programming due to server restrictions.
Can you help me to accomplish this task?
Thanks in advance
Jesus
I have a protocol-kind-of file which contains some entries like these:
WORLD =
(DESCRIPTION =
(DESC =
(ADDRESS = (PROTOCOL = IPC))
(ADDRESS = (PROTOCOL = TCP))
)
)
SERVER_LIST =
(LIST =
(DESCRIPTION =
(NAME = MyFolder)
(PATH = C:\bach\path\home_1)
(folder_name = MyFolder)
(Other line)
)
(DESCRIPTION =
(NAME = YourFolder)
(PATH = C:\bach\path\home_2)
(folder_name = MyFolder)
)
(DESCRIPTION =
(NAME = TheirFolder)
(PATH = C:\bach\path\home_3)
)
)
I want to copy all the content of this file but:
(DESCRIPTION =
(NAME = YourFolder)
(PATH = C:\bach\path\home_2)
(folder_name = MyFolder)
)
to a result file.
I'm thinking on this pseudocode:
1. Get line number containing "TheirFolder" entry -> %L1%
2. Count open (%op%) and close (%cp%) parenthesis from %L1%-1 until %op% - %cp% equals ZERO -> %L2%
3. Copy all that it is before %L1% - 1 to ResultFile.txt
4. Append all that it is after %L2% to ResultFile.txt
This have to be done merely using Windows batch programming due to server restrictions.
Can you help me to accomplish this task?
Thanks in advance
Jesus
Re: How to remove specific text from a protocol file
you want all the content of the file except this part
is that right?
and is it always had these lines in that order like:
(DESCRIPTION =
(NAME = YourFolder)
(PATH = C:\bach\path\home_2)
(folder_name = MyFolder)
)
is that right?
and is it always had these lines in that order like:
0 line --> (
1st Line --> (DESCRIPTION =
2nd Line --> (NAME =
3rd Line --> (PATH =
4th Line --> (folder_name =
5th Line --> )
-
- Posts: 6
- Joined: 21 Sep 2009 15:56
Re: How to remove specific text from a protocol file
abc0502 wrote:you want all the content of the file except this part(DESCRIPTION =
(NAME = YourFolder)
(PATH = C:\bach\path\home_2)
(folder_name = MyFolder)
)
is that right?
and is it always had these lines in that order like:0 line --> (
1st Line --> (DESCRIPTION =
2nd Line --> (NAME =
3rd Line --> (PATH =
4th Line --> (folder_name =
5th Line --> )
No, not always that's why I was thinking about counting open and closing parenthesis. I can have less, more and even different lines for the entry I'm trying to get rid of.
Thanks
Re: How to remove specific text from a protocol file
This works with the sample text - it can be used with head and tail also, and sed, as it extracts the numbers of the lines to remove.
Input file is file.txt
Output is written to file2.txt
Cons: it will corrupt the output if the line starts with a [ or ] character.
Input file is file.txt
Output is written to file2.txt
Cons: it will corrupt the output if the line starts with a [ or ] character.
Code: Select all
@echo off
for /f "skip=2 delims=[]" %%a in ('find /n "(DESCRIPTION =" ^<file.txt') do set top=%%a&goto top:
:top
for /f "skip=3 delims=[]" %%a in ('find /n "(DESCRIPTION =" ^<file.txt') do set bottom=%%a&goto :bottom
:bottom
set /a bottom=bottom-1
del "file2.txt" 2>nul
for /f "tokens=1,* delims=[]" %%a in ('find /n /v "" ^<file.txt') do (
if %%a LSS %top% >>"file2.txt" echo.%%b
if %%a GTR %bottom% >>"file2.txt" echo.%%b
)
-
- Posts: 6
- Joined: 21 Sep 2009 15:56
Re: How to remove specific text from a protocol file
foxidrive wrote:This works with the sample text - it can be used with head and tail also, and sed, as it extracts the numbers of the lines to remove.
Input file is file.txt
Output is written to file2.txt
Cons: it will corrupt the output if the line starts with a [ or ] character.Code: Select all
@echo off
for /f "skip=2 delims=[]" %%a in ('find /n "(DESCRIPTION =" ^<file.txt') do set top=%%a&goto top:
:top
for /f "skip=3 delims=[]" %%a in ('find /n "(DESCRIPTION =" ^<file.txt') do set bottom=%%a&goto :bottom
:bottom
set /a bottom=bottom-1
del "file2.txt" 2>nul
for /f "tokens=1,* delims=[]" %%a in ('find /n /v "" ^<file.txt') do (
if %%a LSS %top% >>"file2.txt" echo.%%b
if %%a GTR %bottom% >>"file2.txt" echo.%%b
)
You're code looks great but I should have done something wrong since I'm receiving ">> was unexpected at this time." I have checked and it's in "if %%a LSS %top% >>"file2.txt" echo.%%b" line. I have tried over and over and I cannot find the solution.
Thanks
Re: How to remove specific text from a protocol file
jjcarranza wrote:foxidrive wrote:You're code looks great but I should have done something wrong since I'm receiving ">> was unexpected at this time." I have checked and it's in "if %%a LSS %top% >>"file2.txt" echo.%%b" line. I have tried over and over and I cannot find the solution.
Did you copy and paste the code from above? Don't forget the very last ) character.
-
- Posts: 6
- Joined: 21 Sep 2009 15:56
Re: How to remove specific text from a protocol file
foxidrive wrote:jjcarranza wrote:foxidrive wrote:You're code looks great but I should have done something wrong since I'm receiving ">> was unexpected at this time." I have checked and it's in "if %%a LSS %top% >>"file2.txt" echo.%%b" line. I have tried over and over and I cannot find the solution.
Did you copy and paste the code from above? Don't forget the very last ) character.
You're right I forgot the closing parenthesis, now it's working. This works great for the example file however it doesn't work when entry to be deleted is located somewhere else. Do you think we can get rid of (NAME = YourFolder) entry when this is located anywhere on the file?
Thanks
Re: How to remove specific text from a protocol file
jjcarranza wrote:This works great for the example file however it doesn't work when entry to be deleted is located somewhere else. Do you think we can get rid of (NAME = YourFolder) entry when this is located anywhere on the file?
Try this:
Code: Select all
@echo off
for /f "delims=[]" %%a in ('find /n /v "" ^<file.txt ^|find /i "(NAME = YourFolder)"') do set c=%%a
set /a bottom=c+3
set /a top=c-1
del "file2.txt" 2>nul
for /f "tokens=1,* delims=[]" %%a in ('find /n /v "" ^<file.txt') do (
if %%a LSS %top% >>"file2.txt" echo.%%b
if %%a GTR %bottom% >>"file2.txt" echo.%%b
)
pause
-
- Posts: 6
- Joined: 21 Sep 2009 15:56
Re: How to remove specific text from a protocol file
foxidrive wrote:jjcarranza wrote:This works great for the example file however it doesn't work when entry to be deleted is located somewhere else. Do you think we can get rid of (NAME = YourFolder) entry when this is located anywhere on the file?
Try this:Code: Select all
@echo off
for /f "delims=[]" %%a in ('find /n /v "" ^<file.txt ^|find /i "(NAME = YourFolder)"') do set c=%%a
set /a bottom=c+3
set /a top=c-1
del "file2.txt" 2>nul
for /f "tokens=1,* delims=[]" %%a in ('find /n /v "" ^<file.txt') do (
if %%a LSS %top% >>"file2.txt" echo.%%b
if %%a GTR %bottom% >>"file2.txt" echo.%%b
)
pause
EXCELLENT: That's what I was looking for, it works like a charm. THANKS!
Re: How to remove specific text from a protocol file
Previous solution is tied to the format of the input file. However...
This means that the program must be modified for the particular format of the input file, and even to have several programs for different input files.
The Batch file below take a "remove file" as a template of the text to remove, so it locate such text in the input file and preserve the rest in the result file. This way, it is very easy to create a new remove template and you may have several remove templates for different input files.
This program eliminate just the first remove block from the file, but it may remove all the blocks by inserting set i=1 line after the last DEL command; it may also eliminate a certain block, or a range of they, with a very simple modification.
The program may also insert an "insert file" in the place of the removed blocks, so it may also serve as a very simple file-edition application.
Antonio
jjcarranza wrote:abc0502 wrote:... is it always had these lines in that order like:0 line --> (
1st Line --> (DESCRIPTION =
2nd Line --> (NAME =
3rd Line --> (PATH =
4th Line --> (folder_name =
5th Line --> )
No, not always that's why I was thinking about counting open and closing parenthesis. I can have less, more and even different lines for the entry I'm trying to get rid of.
This means that the program must be modified for the particular format of the input file, and even to have several programs for different input files.
The Batch file below take a "remove file" as a template of the text to remove, so it locate such text in the input file and preserve the rest in the result file. This way, it is very easy to create a new remove template and you may have several remove templates for different input files.
Code: Select all
@echo off
setlocal EnableDelayedExpansion
rem Load lines from remove file:
set i=0
for /F "delims=" %%a in (remove.txt) do (
set /A i+=1
set "remLine[!i!]=%%a"
)
set remLines=%i%
del remBlock.txt 2>NUL
set i=1
(for /F "delims=" %%a in (input.txt) do (
for %%i in (!i!) do if "%%a" neq "!remLine[%%i]!" (
rem No line match: If there is a previous partial remove block, include it
if exist remBlock.txt (
type remBlock.txt
del remBlock.txt
set i=1
)
echo %%a
) else (
rem Line match: Accumulate it in current remove block
echo %%a>> remBlock.txt
rem If current remove block match whole remove file, eliminate it
set /A i+=1
if !i! gtr %remLines% (
del remBlock.txt
)
)
)) > ResultFile.txt
This program eliminate just the first remove block from the file, but it may remove all the blocks by inserting set i=1 line after the last DEL command; it may also eliminate a certain block, or a range of they, with a very simple modification.
The program may also insert an "insert file" in the place of the removed blocks, so it may also serve as a very simple file-edition application.
Antonio
Re: How to remove specific text from a protocol file
The program compare lines from the input file vs. the remove file. As long as lines match, they are stored in an auxiliary "remove block" file. When one line does not match, the "remove block" is output followed by the unmatched line and the "remove block" is reset. If input lines match the entire remove file, the "remove block" is discarded.
Antonio
remove.txt wrote:(DESCRIPTION =
(NAME = YourFolder)
(PATH = C:\bach\path\home_2)
(folder_name = MyFolder)
)
ResultFile.txt wrote:WORLD =
(DESCRIPTION =
(DESC =
(ADDRESS = (PROTOCOL = IPC))
(ADDRESS = (PROTOCOL = TCP))
)
)
SERVER_LIST =
(LIST =
(DESCRIPTION =
(NAME = MyFolder)
(PATH = C:\bach\path\home_1)
(folder_name = MyFolder)
(Other line)
)
(DESCRIPTION =
(NAME = TheirFolder)
(PATH = C:\bach\path\home_3)
)
)
Antonio