Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Seems to work:</p> <pre><code>@ECHO OFF REM Show usage and quit if no file name was given IF [%1]==[] GOTO USAGE REM Show entire file if no range was given IF [%2]==[] TYPE %1 &amp; GOTO :EOF SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION SET FILENAME=%1 SET LASTLINE=0 REM Build the array of lines to display SHIFT :RANGEREADLOOP CALL :BUILDARRAY %1 SHIFT IF NOT [%1]==[] GOTO RANGEREADLOOP REM Loop through the file and keep track of the lines to display SET CURRENTLINE=0 SET INDEX=1 FOR /F "delims=" %%l in (%FILENAME%) DO ( SET LINE=%%l CALL :PRINTLINE ) GOTO :EOF REM Adds the lines from the specified range to the array of lines to display :BUILDARRAY REM Find out whether we have a single line or a range SET TEST=%1 SET /A TEST1=%TEST% SET /A TEST2=%TEST:-=% IF %TEST1%==%TEST2% ( REM Single line SET /A LASTLINE+=1 SET LINES[!LASTLINE!]=%1 ) ELSE ( REM Range FOR /F "tokens=1,2 delims=-" %%x IN ("%1") DO (SET RANGESTART=%%x&amp;SET RANGEEND=%%y) REM Some sanity checking IF !RANGESTART! GTR !RANGEEND! ( ECHO.Problem with range !RANGESTART!-!RANGEEND!: ECHO. Ranges must have a start value smaller than the end value. EXIT /B 1 ) ELSE ( FOR /L %%i IN (!RANGESTART!,1,!RANGEEND!) DO ( SET /A LASTLINE+=1 SET LINES[!LASTLINE!]=%%i ) ) ) GOTO :EOF REM Prints the specified line if the current line should be printed :PRINTLINE SET /A CURRENTLINE+=1 IF %CURRENTLINE%==!LINES[%INDEX%]! ( REM We have a line to print, so do this ECHO !LINE! SET /A INDEX+=1 ) GOTO :EOF REM Prints usage and exits the batch file :USAGE ECHO.%~n0 - Displays selected lines from a text file. ECHO. ECHO.Usage: ECHO. %~n0 ^&lt;filename^&gt; ^&lt;range^&gt; ... ECHO. ECHO. ^&lt;filename^&gt; - the text file from which to read ECHO. ^&lt;range^&gt; - the line range(s) to display. ECHO. ECHO.Example: ECHO. %~n0 foo.txt 1-4 13 15 18-20 ECHO. ECHO. will display the first four lines from the file "foo.txt", ECHO. the 13th and 15th as well as the lines 18 to 20. ECHO. ECHO.Line ranges are separated by comma, semicolon or space. If no range is given, ECHO.the entire file is displayed. EXIT /B 1 GOTO :EOF </code></pre> <p>The whole script could use some better error checking, examples of what not to do or where error checking is a bit wonky:</p> <ul> <li><code>dl foo.txt 1-2-4</code> (prints lines 1-2 but no error message)</li> <li><code>dl foo.txt -1</code> (error message that the range 1- isn't correct)</li> </ul> <p>Other limitations:</p> <ul> <li>Ranges must be sorted. With the current implementation there is no way to do something like <code>dl foo.txt 7-8,1-2</code>.</li> <li>No line may be selected twice. Something like <code>dl foo.txt 1,2,2-8,11-15</code> will stop after the second line.</li> <li>No support for UNIX-style line endings (U+000A)</li> </ul> <p><strong>EDIT:</strong> Fixed an issue with files that contain parentheses.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload