Cut off block based on certain strings in a file
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a task to cut off the block starting from certain string in the middle of a file and then to save this upper part of the file into a new file.
;---- Some text A
aaaaaaa1111
……..
aaaaaaa2222
;-----Some text B
bbbbbbbbb1111
…………..
bbbbbbbbb2222
Now I am looking for a batch file to cut off the part starting from:
;-----Some text B
So after cut, the new file looks like:
;---- Some text A
aaaaaaa1111
……..
aaaaaaa2222
All the part starting from:
;-----Some text B
bbbbbbbbb1111
…………..
bbbbbbbbb2222
has been cut off.
I tried to use the Function from DOStip then call that function as below:
@ECHO OFF
REM.-- Prepare the Command Processor
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
echo.Extracting hello world block into "Load_SDK.M83" file
call:extractFromFile ";---- Some text A" ";---- Some text B">"Load_SDK.M83"
:extractFromFile - extract lines from a file between begin and end mark
:: - %~1: begin mark, use '...$' mark to allow variable substitution
:: - %~2: optional end mark, default is end of file
:: - %~3: optional source file, default is THIS file
SETLOCAL
set bmk=;---- Some text A
set emk=;---- Some text B
set src=C:FilesLoad_all.M83
set /a b=-1
set /a e=-1
if "%src%"=="" set src=%~f0& ::- if no source file then assume THIS file
for /f "tokens=1,* delims=:" %%a in ('"findstr /n /b /c:"%bmk%" "%~f0""') do (
set b=%%a
set bmk=%%b
)
if /i %b%==-1 echo.ERROR: begin mark '%bmk%' not found in '%src%'&GOTO:EOF
if "%emk%"=="" (set /a e=2000000000) ELSE (
for /f "delims=:" %%a in ('"findstr /n /b /c:"%emk%" "%~f0""') do (
if /i %b% LSS %%a if /i !e!==-1 set e=%%a& rem -- find only the first one after b
)
)
if /i %e%==-1 echo.ERROR: end mark '%emk%' missing in '%src%'&GOTO:EOF
if /i %b% GEQ %e% echo.ERROR: end mark '%emk%' detected before begin mark '%bmk%' in '%src%'&GOTO:EOF
for /f "delims=: tokens=1,*" %%a in ('"findstr /v /n /b /c:"#$*ReturnAll*$#" "%src%""') do (
if /i %b% LSS %%a if /i %%a LSS %e% (
if "%bmk:~-1%"=="$" (
rem --sustitution variables within %%b
call echo.%%b
) ELSE (
rem --no variable substitution
echo.%%b
)
)
)
GOTO:EOF
batch-file
add a comment |
I have a task to cut off the block starting from certain string in the middle of a file and then to save this upper part of the file into a new file.
;---- Some text A
aaaaaaa1111
……..
aaaaaaa2222
;-----Some text B
bbbbbbbbb1111
…………..
bbbbbbbbb2222
Now I am looking for a batch file to cut off the part starting from:
;-----Some text B
So after cut, the new file looks like:
;---- Some text A
aaaaaaa1111
……..
aaaaaaa2222
All the part starting from:
;-----Some text B
bbbbbbbbb1111
…………..
bbbbbbbbb2222
has been cut off.
I tried to use the Function from DOStip then call that function as below:
@ECHO OFF
REM.-- Prepare the Command Processor
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
echo.Extracting hello world block into "Load_SDK.M83" file
call:extractFromFile ";---- Some text A" ";---- Some text B">"Load_SDK.M83"
:extractFromFile - extract lines from a file between begin and end mark
:: - %~1: begin mark, use '...$' mark to allow variable substitution
:: - %~2: optional end mark, default is end of file
:: - %~3: optional source file, default is THIS file
SETLOCAL
set bmk=;---- Some text A
set emk=;---- Some text B
set src=C:FilesLoad_all.M83
set /a b=-1
set /a e=-1
if "%src%"=="" set src=%~f0& ::- if no source file then assume THIS file
for /f "tokens=1,* delims=:" %%a in ('"findstr /n /b /c:"%bmk%" "%~f0""') do (
set b=%%a
set bmk=%%b
)
if /i %b%==-1 echo.ERROR: begin mark '%bmk%' not found in '%src%'&GOTO:EOF
if "%emk%"=="" (set /a e=2000000000) ELSE (
for /f "delims=:" %%a in ('"findstr /n /b /c:"%emk%" "%~f0""') do (
if /i %b% LSS %%a if /i !e!==-1 set e=%%a& rem -- find only the first one after b
)
)
if /i %e%==-1 echo.ERROR: end mark '%emk%' missing in '%src%'&GOTO:EOF
if /i %b% GEQ %e% echo.ERROR: end mark '%emk%' detected before begin mark '%bmk%' in '%src%'&GOTO:EOF
for /f "delims=: tokens=1,*" %%a in ('"findstr /v /n /b /c:"#$*ReturnAll*$#" "%src%""') do (
if /i %b% LSS %%a if /i %%a LSS %e% (
if "%bmk:~-1%"=="$" (
rem --sustitution variables within %%b
call echo.%%b
) ELSE (
rem --no variable substitution
echo.%%b
)
)
)
GOTO:EOF
batch-file
1
Just so everyone knows, the OP has got extensive help on this question already at DosTips.com
– Squashman
Nov 26 '18 at 19:40
2
Without us having to go off site, can you please provide a genuine example of the file content, what you require as an end result based upon it, the script you've used in your attempts to achieve it, and how that script fails to perform the task it was written to perform.
– Compo
Nov 26 '18 at 20:35
I note that the content of your file does not match the provided code. We need either genuine content with code written for it, or generic content, (which means that solutions may not work with the actual file content), and code written to match. The code you've added as an edit does not represent the task as you've explained it in your question, which seems to require only a singleFor
loop with anIf
and aGoTo
. Please try harder to produce a question which doesn't require a whole lot more questions.
– Compo
Nov 26 '18 at 22:58
add a comment |
I have a task to cut off the block starting from certain string in the middle of a file and then to save this upper part of the file into a new file.
;---- Some text A
aaaaaaa1111
……..
aaaaaaa2222
;-----Some text B
bbbbbbbbb1111
…………..
bbbbbbbbb2222
Now I am looking for a batch file to cut off the part starting from:
;-----Some text B
So after cut, the new file looks like:
;---- Some text A
aaaaaaa1111
……..
aaaaaaa2222
All the part starting from:
;-----Some text B
bbbbbbbbb1111
…………..
bbbbbbbbb2222
has been cut off.
I tried to use the Function from DOStip then call that function as below:
@ECHO OFF
REM.-- Prepare the Command Processor
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
echo.Extracting hello world block into "Load_SDK.M83" file
call:extractFromFile ";---- Some text A" ";---- Some text B">"Load_SDK.M83"
:extractFromFile - extract lines from a file between begin and end mark
:: - %~1: begin mark, use '...$' mark to allow variable substitution
:: - %~2: optional end mark, default is end of file
:: - %~3: optional source file, default is THIS file
SETLOCAL
set bmk=;---- Some text A
set emk=;---- Some text B
set src=C:FilesLoad_all.M83
set /a b=-1
set /a e=-1
if "%src%"=="" set src=%~f0& ::- if no source file then assume THIS file
for /f "tokens=1,* delims=:" %%a in ('"findstr /n /b /c:"%bmk%" "%~f0""') do (
set b=%%a
set bmk=%%b
)
if /i %b%==-1 echo.ERROR: begin mark '%bmk%' not found in '%src%'&GOTO:EOF
if "%emk%"=="" (set /a e=2000000000) ELSE (
for /f "delims=:" %%a in ('"findstr /n /b /c:"%emk%" "%~f0""') do (
if /i %b% LSS %%a if /i !e!==-1 set e=%%a& rem -- find only the first one after b
)
)
if /i %e%==-1 echo.ERROR: end mark '%emk%' missing in '%src%'&GOTO:EOF
if /i %b% GEQ %e% echo.ERROR: end mark '%emk%' detected before begin mark '%bmk%' in '%src%'&GOTO:EOF
for /f "delims=: tokens=1,*" %%a in ('"findstr /v /n /b /c:"#$*ReturnAll*$#" "%src%""') do (
if /i %b% LSS %%a if /i %%a LSS %e% (
if "%bmk:~-1%"=="$" (
rem --sustitution variables within %%b
call echo.%%b
) ELSE (
rem --no variable substitution
echo.%%b
)
)
)
GOTO:EOF
batch-file
I have a task to cut off the block starting from certain string in the middle of a file and then to save this upper part of the file into a new file.
;---- Some text A
aaaaaaa1111
……..
aaaaaaa2222
;-----Some text B
bbbbbbbbb1111
…………..
bbbbbbbbb2222
Now I am looking for a batch file to cut off the part starting from:
;-----Some text B
So after cut, the new file looks like:
;---- Some text A
aaaaaaa1111
……..
aaaaaaa2222
All the part starting from:
;-----Some text B
bbbbbbbbb1111
…………..
bbbbbbbbb2222
has been cut off.
I tried to use the Function from DOStip then call that function as below:
@ECHO OFF
REM.-- Prepare the Command Processor
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
echo.Extracting hello world block into "Load_SDK.M83" file
call:extractFromFile ";---- Some text A" ";---- Some text B">"Load_SDK.M83"
:extractFromFile - extract lines from a file between begin and end mark
:: - %~1: begin mark, use '...$' mark to allow variable substitution
:: - %~2: optional end mark, default is end of file
:: - %~3: optional source file, default is THIS file
SETLOCAL
set bmk=;---- Some text A
set emk=;---- Some text B
set src=C:FilesLoad_all.M83
set /a b=-1
set /a e=-1
if "%src%"=="" set src=%~f0& ::- if no source file then assume THIS file
for /f "tokens=1,* delims=:" %%a in ('"findstr /n /b /c:"%bmk%" "%~f0""') do (
set b=%%a
set bmk=%%b
)
if /i %b%==-1 echo.ERROR: begin mark '%bmk%' not found in '%src%'&GOTO:EOF
if "%emk%"=="" (set /a e=2000000000) ELSE (
for /f "delims=:" %%a in ('"findstr /n /b /c:"%emk%" "%~f0""') do (
if /i %b% LSS %%a if /i !e!==-1 set e=%%a& rem -- find only the first one after b
)
)
if /i %e%==-1 echo.ERROR: end mark '%emk%' missing in '%src%'&GOTO:EOF
if /i %b% GEQ %e% echo.ERROR: end mark '%emk%' detected before begin mark '%bmk%' in '%src%'&GOTO:EOF
for /f "delims=: tokens=1,*" %%a in ('"findstr /v /n /b /c:"#$*ReturnAll*$#" "%src%""') do (
if /i %b% LSS %%a if /i %%a LSS %e% (
if "%bmk:~-1%"=="$" (
rem --sustitution variables within %%b
call echo.%%b
) ELSE (
rem --no variable substitution
echo.%%b
)
)
)
GOTO:EOF
batch-file
batch-file
edited Nov 28 '18 at 17:43
double-beep
3,12141532
3,12141532
asked Nov 26 '18 at 19:36
goodywpgoodywp
166
166
1
Just so everyone knows, the OP has got extensive help on this question already at DosTips.com
– Squashman
Nov 26 '18 at 19:40
2
Without us having to go off site, can you please provide a genuine example of the file content, what you require as an end result based upon it, the script you've used in your attempts to achieve it, and how that script fails to perform the task it was written to perform.
– Compo
Nov 26 '18 at 20:35
I note that the content of your file does not match the provided code. We need either genuine content with code written for it, or generic content, (which means that solutions may not work with the actual file content), and code written to match. The code you've added as an edit does not represent the task as you've explained it in your question, which seems to require only a singleFor
loop with anIf
and aGoTo
. Please try harder to produce a question which doesn't require a whole lot more questions.
– Compo
Nov 26 '18 at 22:58
add a comment |
1
Just so everyone knows, the OP has got extensive help on this question already at DosTips.com
– Squashman
Nov 26 '18 at 19:40
2
Without us having to go off site, can you please provide a genuine example of the file content, what you require as an end result based upon it, the script you've used in your attempts to achieve it, and how that script fails to perform the task it was written to perform.
– Compo
Nov 26 '18 at 20:35
I note that the content of your file does not match the provided code. We need either genuine content with code written for it, or generic content, (which means that solutions may not work with the actual file content), and code written to match. The code you've added as an edit does not represent the task as you've explained it in your question, which seems to require only a singleFor
loop with anIf
and aGoTo
. Please try harder to produce a question which doesn't require a whole lot more questions.
– Compo
Nov 26 '18 at 22:58
1
1
Just so everyone knows, the OP has got extensive help on this question already at DosTips.com
– Squashman
Nov 26 '18 at 19:40
Just so everyone knows, the OP has got extensive help on this question already at DosTips.com
– Squashman
Nov 26 '18 at 19:40
2
2
Without us having to go off site, can you please provide a genuine example of the file content, what you require as an end result based upon it, the script you've used in your attempts to achieve it, and how that script fails to perform the task it was written to perform.
– Compo
Nov 26 '18 at 20:35
Without us having to go off site, can you please provide a genuine example of the file content, what you require as an end result based upon it, the script you've used in your attempts to achieve it, and how that script fails to perform the task it was written to perform.
– Compo
Nov 26 '18 at 20:35
I note that the content of your file does not match the provided code. We need either genuine content with code written for it, or generic content, (which means that solutions may not work with the actual file content), and code written to match. The code you've added as an edit does not represent the task as you've explained it in your question, which seems to require only a single
For
loop with an If
and a GoTo
. Please try harder to produce a question which doesn't require a whole lot more questions.– Compo
Nov 26 '18 at 22:58
I note that the content of your file does not match the provided code. We need either genuine content with code written for it, or generic content, (which means that solutions may not work with the actual file content), and code written to match. The code you've added as an edit does not represent the task as you've explained it in your question, which seems to require only a single
For
loop with an If
and a GoTo
. Please try harder to produce a question which doesn't require a whole lot more questions.– Compo
Nov 26 '18 at 22:58
add a comment |
1 Answer
1
active
oldest
votes
Based on my comment, a one line single For
loop with an If
and a GoTo
seems to perform the task as shown in your question:
@(For /F UseBackQDelims^=^ EOL^= %%A In ("source.txt") Do @If /I Not "%%A"==";-----Some text B" (Set /P "=%%A"<Nul&Echo=) Else GoTo :EOF)>"result.txt"
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487877%2fcut-off-block-based-on-certain-strings-in-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Based on my comment, a one line single For
loop with an If
and a GoTo
seems to perform the task as shown in your question:
@(For /F UseBackQDelims^=^ EOL^= %%A In ("source.txt") Do @If /I Not "%%A"==";-----Some text B" (Set /P "=%%A"<Nul&Echo=) Else GoTo :EOF)>"result.txt"
add a comment |
Based on my comment, a one line single For
loop with an If
and a GoTo
seems to perform the task as shown in your question:
@(For /F UseBackQDelims^=^ EOL^= %%A In ("source.txt") Do @If /I Not "%%A"==";-----Some text B" (Set /P "=%%A"<Nul&Echo=) Else GoTo :EOF)>"result.txt"
add a comment |
Based on my comment, a one line single For
loop with an If
and a GoTo
seems to perform the task as shown in your question:
@(For /F UseBackQDelims^=^ EOL^= %%A In ("source.txt") Do @If /I Not "%%A"==";-----Some text B" (Set /P "=%%A"<Nul&Echo=) Else GoTo :EOF)>"result.txt"
Based on my comment, a one line single For
loop with an If
and a GoTo
seems to perform the task as shown in your question:
@(For /F UseBackQDelims^=^ EOL^= %%A In ("source.txt") Do @If /I Not "%%A"==";-----Some text B" (Set /P "=%%A"<Nul&Echo=) Else GoTo :EOF)>"result.txt"
answered Nov 26 '18 at 23:55
CompoCompo
17.2k3927
17.2k3927
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487877%2fcut-off-block-based-on-certain-strings-in-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Just so everyone knows, the OP has got extensive help on this question already at DosTips.com
– Squashman
Nov 26 '18 at 19:40
2
Without us having to go off site, can you please provide a genuine example of the file content, what you require as an end result based upon it, the script you've used in your attempts to achieve it, and how that script fails to perform the task it was written to perform.
– Compo
Nov 26 '18 at 20:35
I note that the content of your file does not match the provided code. We need either genuine content with code written for it, or generic content, (which means that solutions may not work with the actual file content), and code written to match. The code you've added as an edit does not represent the task as you've explained it in your question, which seems to require only a single
For
loop with anIf
and aGoTo
. Please try harder to produce a question which doesn't require a whole lot more questions.– Compo
Nov 26 '18 at 22:58