
Could someone here help me?
Please note that my question is limited to findstr, because such a solution could be used for binary files too (which are my real target).
Thank you in advance.
Moderator: DosItHelp
Code: Select all
@echo off
setlocal enabledelayedexpansion
:: Specify which line to start returning from
set get_line=7
:: Specify how many lines to return
set return_lines=5
:: In this example, lines 7 through 12 will be returned
:: Trial and error made me add this line. Don't touch it.
set /a get_line-=2
(
for /L %%A in (0,1,%get_line%) do set /p skip_line=
for /L %%A in (0,1,%return_count%) do (
set /p print_line=
echo !print_line!
)
) <file.txt
Code: Select all
type binary_file.exe >file.txt
Code: Select all
#include <stdio.h>
int main(int argc, char **argv) {
FILE *fp, *ofp;
long si,ei;
if (argc <= 3) { puts("Usage: cpbinl file startline endline"); return 1; }
si=atoi(argv[2]); ei=atoi(argv[3]);
if (si < 1 || ei < 1 || ei < si) { puts("Invalid index"); return 1; }
fp=fopen(argv[1], "rb");
argv[1][0]='#'; if (fp) ofp=fopen(argv[1], "wb");
if (fp && ofp) {
long line=1, read;
unsigned char ch;
do {
read=fread(&ch, 1, 1, fp);
if (read && line >= si && line <= ei) fwrite(&ch, 1, 1, ofp);
if (ch == 0xa) line++;
} while(read);
if (fp) fclose(fp);
if (ofp) fclose(ofp);
} else puts("File error");
return 0;
}
If the lines does not have leading white spaces or equal sign(=) `set /p "=!var!"<nul>outFile` can be used for that matter. On Vista and beyond set /p removes leading white spaces from prompt string.
Code: Select all
setlocal
.
.
set "prompt=!var:$=$$!"
cmd /d /k <nul>outFile
.
.
endlocal
Code: Select all
for /L %%A in (0,1,%return_count%) do (
set /p print_line=
set "prompt=!print_line:$=$$!"
cmd /d /k <nul
if %%A LSS %return_count% echo,
)
That is not necessarily true. As far as I'm concerned, findstr on it's own, does not have the capability to filter specific line numbers. So the assumption that any solution of your task involving findstr, can automatically handle binary files is not true.
Code: Select all
@echo off
setlocal EnableExtensions DisableDelayedExpansion
:: Parameters
set "Input=input.txt"
set "Output=output.txt"
:: The minimum value for startLine is 1
set /a "startLine=5, endLine=9"
set /a "startLine-=1"
if %startLine% EQU 0 (set "skip=") else set "skip=skip=%startLine%"
(for /F "%skip% tokens=1* delims=:" %%K in ('findstr /N /R "^" "%Input%"') do (
if %%K LEQ %endLine% (echo(%%L)
))>"%Output%"
Code: Select all
>"%Output%" cmd /e:on /v:off /d /c for /F "%skip% tokens=1* delims=:" %%K in ^('findstr /N /R "^" "%Input%"'^) do @if %%K LEQ %endLine% ^(echo(%%L^) else exit
Code: Select all
@echo off
setlocal EnableExtensions DisableDelayedExpansion
:: Parameters
set "Input=input.bin"
set "Output=output.bin"
set /a "startLine=5, endLine=9"
set /a "cureLine=startLine-1, Lines=endLine-startLine+1, MaxDigits=9"
(for /L %%. in (1,1,%Lines%) do (
set /a "cureLine+=1, Num=cureLine, LeadChars=1"
for /L %%. in (1,1,%MaxDigits%) do set /a "LeadChars+=!!Num, Num/=10"
findstr /N /R "^" "%Input%"|(findstr /R "^%%cureLine%%:")|((for /L %%. in (1,1,%%LeadChars%%) do @pause)>nul & findstr /R "^")
REM // This line takes into account the possibility for disabled command extensions. Use instead of above if that is a concern.
REM findstr /N /R "^" "%Input%"|(findstr /R "^%%cureLine%%:")|cmd /e:on /d /c ^(for /L %%^^^. in ^(1,1,%%LeadChars%%^) do @pause^)^>nul ^& findstr /R "^"
))>"%Output%"
Be aware that this method is extremely slow and inefficient in terms of performance but should do the job. That was the best I could come up with.