0

バッチ ファイルのテキスト ファイルから「linenumber# and linenumber# as lines#-#」を表示できるスクリプトを見つけようとしています。私はこのサイトでこのスクリプトを見つけました..

@echo off
setlocal enabledelayedexpansion
if [%1] == [] goto usage
if [%2] == [] goto usage

SET /a counter=0

for /f "usebackq delims=" %%a in (%2) do (
if "!counter!"=="%1" goto exit
echo %%a
set /a counter+=1
)

goto exit

:usage
echo Usage: head.bat COUNT FILENAME

:exit

そして、それはうまく機能します:)しかし、テキストファイルの先頭から行数を取得します。テキストファイルの特定の行と、可能であれば範囲を表示できるようにしたい..

例: 30 行のテキスト ファイルがあり、1 ~ 4 行を表示したいと考えています。7-11; 13; 17-20; 22; 26 & 29

4

2 に答える 2

2

上記のサンプル バッチ ファイルを簡単に変更すると、次のようになります。以下のコードをファイルにコピーし、「LineDisplay.bat」という名前を付けます。パラメータとして FirstLineNumber と LastLineNumber を取ります。例: LineDisplay test.txt 12 30 (12 ~ 30 行を読むため)

@echo off
setlocal enabledelayedexpansion
if [%1] == [] goto usage
if [%2] == [] goto usage
if [%3] == [] goto usage

set /a FirstLineNumber = %2
set /a LastLineNumber = %3

echo Reading from Line !FirstLineNumber! to !LastLineNumber!


SET /a counter=1

for /f "usebackq delims=" %%a in (%1) do (
    if !counter! GTR !LastLineNumber! goto exit
    if !counter! GEQ !FirstLineNumber! echo !counter! %%a
    set /a counter+=1
)

goto exit

:usage
echo Usage: LineDisplay.bat FILENAME FirstLineNumber LastLineNumber

:exit

バッチファイルの作成に関する素晴らしいチュートリアルへの行は次のとおりですhttp://vtatila.kapsi.fi/batch_tutorial.html

于 2009-04-13T16:03:00.883 に答える
0

うまくいくようです:

@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 & 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&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 ^<filename^> ^<range^> ...
    ECHO.
    ECHO.  ^<filename^> - the text file from which to read
    ECHO.  ^<range^>    - 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

スクリプト全体で、より優れたエラー チェックを使用できます。たとえば、何をすべきでないか、またはエラー チェックが少し不安定な場合です。

  • dl foo.txt 1-2-4 (行 1 ~ 2 を出力しますが、エラー メッセージは表示されません)
  • dl foo.txt -1 (範囲 1- が正しくないというエラー メッセージ)

その他の制限:

  • 範囲はソートする必要があります。現在の実装では、 のようなことを行う方法はありませんdl foo.txt 7-8,1-2
  • 行を 2 回選択することはできません。のようなものdl foo.txt 1,2,2-8,11-15が 2 行目以降で停止します。
  • UNIX スタイルの行末 (U+000A) はサポートされていません

編集:括弧を含むファイルの問題を修正しました。

于 2009-04-13T16:01:15.583 に答える