Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Text 在批处理文件中显示文本文件中的行_Text_Batch File_Numbers_Lines - Fatal编程技术网

Text 在批处理文件中显示文本文件中的行

Text 在批处理文件中显示文本文件中的行,text,batch-file,numbers,lines,Text,Batch File,Numbers,Lines,我正在试图找到一个脚本,它可以让我在批处理文件中的文本文件中显示“linenumber”和linenumber以及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!"==

我正在试图找到一个脚本,它可以让我在批处理文件中的文本文件中显示“linenumber”和linenumber以及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似乎有效:

@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
@ECHO关闭
REM显示使用情况,如果未提供文件名,则退出
如果[%1]=[]转到使用情况
如果未指定范围,REM将显示整个文件
如果[%2]=[]类型%1和转到:EOF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
设置文件名=%1
设置LASTLINE=0
REM构建要显示的行数组
移位
:RANGEREADLOOP
调用:BUILDARRAY%1
移位
如果不是[%1]=[]转到RANGEREADLOOP
REM循环文件并跟踪要显示的行
设置CURRENTLINE=0
设置索引=1
对于/F“delims=“%%l in(%FILENAME%)DO(
设置行=%%l
呼叫:打印行
)
后藤:EOF
REM将指定范围内的行添加到要显示的行数组中
:BUILDARRAY
REM找出我们是有一条直线还是一个范围
设置测试=%1
设置/A测试1=%TEST%
SET/A TEST2=%TEST:-=%
如果%TEST1%==%TEST2%(
REM单线
设置/A最后一行+=1
设置行[!LASTLINE!]=%1
)否则(
REM范围
对于/F“标记=1,2 delims=-”%%x IN(“%1”)DO(设置RANGESTART=%%x&设置RANGEEND=%%y)
雷姆做了一些精神检查
如果!RANGESTART!GTR!RANGEEND(
回声。范围有问题!RANGESTART!-!RANGEEND!:
范围的起始值必须小于结束值。
退出/B 1
)否则(
对于(!RANGESTART!、1、!RANGEEND!)中的/L%%i,请执行以下操作(
设置/A最后一行+=1
设置行[!LASTLINE!]=%%i
)
)
)
后藤:EOF
如果应打印当前行,REM将打印指定行
:打印行
设置/A电流线+=1
如果%CURRENTLINE%==!行[%INDEX%]!(
REM我们有一行要打印,所以请这样做
回声!线路!
集合/A索引+=1
)
后藤:EOF
REM打印使用情况并退出批处理文件
:用法
ECHO.%~n0-显示文本文件中选定的行。
回声。
ECHO.用法:
回声.%~不^ ^。。。
回声。
回声。^-要从中读取的文本文件
回声。^-要显示的行范围。
回声。
ECHO.例如:
回声.%~n0 foo.txt 1-4 13 15 18-20
回声。
回声。将显示文件“foo.txt”的前四行,
回声。第13行和第15行以及第18到20行。
回声。
回音。行范围由逗号、分号或空格分隔。如果没有给出范围,
将显示整个文件。
退出/B 1
后藤:EOF
整个脚本可以使用一些更好的错误检查,例如不做什么或错误检查有点不可靠:

  • dl foo.txt 1-2-4
    (打印第1-2行,但没有错误消息)
  • dl foo.txt-1
    (错误消息,范围1-不正确)
其他限制:

  • 必须对范围进行排序。对于当前的实现,无法执行类似于
    dl foo.txt 7-8,1-2
    的操作
  • 不能选择任何行两次。类似于
    dl foo.txt 1,2,2-8,11-15的内容将在第二行之后停止
  • 不支持UNIX样式的行尾(U+000A)

编辑:修复了包含括号的文件的问题。

这里是对上述示例批处理文件的简单修改。将下面的代码复制到文件中,并将其命名为“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