需要在批处理文件中从a行读取到b行 [英] need to read from line a to line b in a batch file

查看:75
本文介绍了需要在批处理文件中从a行读取到b行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@echo off
cls
Color 0A

:Read
setlocal EnableDelayedExpansion
set file=WinSCP-5.11.2-ReadMe.txt
call :ReadInLines
call :EchoLines

echo insert other code here
pause
endlocal
(goto) 2>nul

:ReadInLines
set Counter=0
for /f "DELIMS=" %%i in ('type %file%') do (
    set /a Counter+=1
    title Lines In File: !Counter!
    set "Line_!Counter!=%%i"
)
(goto) 2>nul

:EchoLines
For /L %%C in (1,1,%Counter%) Do (echo %%C. !Line_%%C!)
pause

所以这是我目前的代码

它能够成功读取和输出整个文件,但是我想以一种不需要滚动的方式工作(对于较大的文件)

It is able to successfully read and output the file as whole but I want to work on a way that doesn't require scrolling (for bigger files)

基本上,我需要做的是读取一定数量的行

basically what i need to be able to do is read a certain amount of lines

示例:

读取第1至8行,但也读取第5至13行

read lines 1 to 8 but also read lines 5 to 13

基本上,我需要能够将其读取的数字互换,最好使用要呼叫的标签

basically i need to be able to interchange the numbers it reads to, a label to call is preferred

完成的代码:

call.bat:

@echo off
cls
Color 0A

:: double call
call text-read-lines-alt.bat 1 8
call text-read-lines-alt.bat 9 20
pause

:: multiselect (a) (b) (a) (b) etc...
call text-read-lines-alt.bat 1 20 23 29
pause

:: call then calling outside text range (outputs first but not second)
call text-read-lines-alt.bat 1 8
call text-read-lines-alt.bat 80 100
pause

:: another call outside text range (outputs nothing)
call text-read-lines-alt.bat 90 100
pause

text-read-lines-alt.bat:

text-read-lines-alt.bat:

@echo off
setlocal EnableDelayedExpansion
set file=WinSCP-5.11.2-ReadMe.txt

SET "parms=%*"

call :ReadInLines
call :EchoLines

echo insert other code here
endlocal
(goto) 2>nul

:ReadInLines
set Counter=0
for /f "DELIMS=" %%i in ('type %file%') do (
    set /a Counter+=1
    title Lines In File: !Counter!

    CALL :gate !counter!

    IF DEFINED RECORD set "Line_!Counter!=%%i"
)
(goto) 2>nul

:EchoLines
For /L %%C in (1,1,%Counter%) Do IF DEFINED line_%%C (echo %%C. !Line_%%C!)
GOTO :EOF

:gate
SET "record="
IF NOT DEFINED parms GOTO :EOF 
FOR /f "tokens=1,2*" %%x IN ("%parms%") DO (
    IF %1 gtr %%y SET "parms=%%z"&GOTO gate
    IF %1 geq %%x SET "record=Y"
)
GOTO :EOF

这是所有辛苦工作的结果: http://old-school-gamer.tk/batch/text-reader/releases/ :D

and here is what all the hard work went to: http://old-school-gamer.tk/batch/text-reader/releases/ :D

推荐答案

@ECHO OFF
setlocal EnableDelayedExpansion
set file=WinSCP-5.11.2-ReadMe.txt
set file=100lines.txt

SET "parms=%*"

call :ReadInLines
call :EchoLines

echo insert other code here
pause
endlocal
(goto) 2>nul

:ReadInLines
set Counter=0
for /f "DELIMS=" %%i in ('type %file%') do (
    set /a Counter+=1
    title Lines In File: !Counter!

    CALL :gate !counter!

    IF DEFINED RECORD set "Line_!Counter!=%%i"
)
(goto) 2>nul

:EchoLines
For /L %%C in (1,1,%Counter%) Do IF DEFINED line_%%C (echo %%C. !Line_%%C!)
SET /a count=0
For /L %%C in (1,1,%Counter%) Do IF DEFINED line_%%C (
 SET /a count +=1
 echo !count!. !Line_%%C!
)
GOTO :EOF

:gate
SET "record="
IF NOT DEFINED parms GOTO :EOF 
FOR /f "tokens=1,2*" %%x IN ("%parms%") DO (
 IF %1 gtr %%y SET "parms=%%z"&GOTO gate
 IF %1 geq %%x SET "record=Y"
)
GOTO :EOF

我首先记录了parms = command-tail.我的100lines.txt文件就是一个包含100行第1行" .."line100"

I started by recording parms=the command-tail. My 100lines.txt file is simply a file containing 100 lines "line 1".."line100"

CALL设置或清除record,以指定是否应记录counter中的行. if defined处理目标变量的当前状态-是否已定义.

CALLing the :gate procedure sets or clears record to specify whether the line in counter should be recorded or not. if defined works on the current status of the target variable - defined or not.

:gate例程检查parms字符串,将%%x分配给第一个标记,将%%y分配给第二个标记,并将%%z分配给所提供的其余参数.如果%1中的当前行号(从调用循环开始)大于第二个参数,则将parms分配给字符串的其余部分(将删除前两个参数),然后重试. parms最终将变为空,因此不要尝试对其进行处理.

The :gate routine examines the parms string, assigning %%x to the first token, %%y to the second and %%z to the remainder of the parameters provided. If the current line number in %1 (from the calling loop) is greater then the second parameter, then assign parms to the rest of the string (which removes thefirst two parameters) and try again. parms will eventually become empty, so simply don't try processing it.

如果第二个参数不大于当前行,请查看当前行是否大于或等于第一个参数.如果是这样,请将record设置为一个值,使其成为定义的,因此调用循环将记录该值.

If the second parameter is not greater than the current line, see whether the current line is greater than or equal to the first parameter. If so, set record to a value so it becomes defined and hence the calling loop will record it.

所以-假设params10 13 21 28. %%x将被设置为10%%y被设置为13%%z被设置为21 28.在第9行之前,行号不会是geq 10,因此record仍然清晰可见.对于第10到13行,将设置record,并且在第14、14行上记录的行大于13,因此parms变为21 28,然后重试.

So - say params is 10 13 21 28. %%x will be set to 10, %%y to 13 and %%z to 21 28. Until line 9, the line number will not be geq 10, so record remains clear. For lines 10 to 13, record will be set and the line recorded, on line 14, 14 is greater than 13, so parms becomes 21 28 and we try again.

我也修改了输出过程.如果定义了line_%%C,则if defined门将仅执行echo,因此将没有空行.缺点是报告将显示文件中的行号.

I've modified the output procedure too. the if defined gate will only execute the echo if line_%%C is defined, so there'll be no empty lines. The downside is that the report will show the line number from the file.

第二个过程使用了一种相当明显的方法来对输出进行序列化以产生行号.

The second procedure uses a fairly obvious method to serialise the output to produce the line numbers.

例如,

如果数据文件包含

第1行
第2行
第3行
第4行
第5行
第6行
第7行
第8行

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8

然后,如果提供的参数是3 5,表示包含第3至5行",则输出来自

then if the parameters supplied are 3 5 meaning "lines 3 to 5 inclusive" then the output from

For /L %%C in (1,1,%Counter%) Do IF DEFINED line_%%C (echo %%C. !Line_%%C!)

将是

  1. 第3行
  2. 第4行
  3. 第5行
  1. Line 3
  2. Line 4
  3. Line 5

(请注意,每行都有其原始行号.

(note each line has its original line number.

和来自

SET /a count=0
For /L %%C in (1,1,%Counter%) Do IF DEFINED line_%%C (
 SET /a count +=1
 echo !count!. !Line_%%C!
)

将是

  1. 第3行
  2. 第4行
  3. 第5行
  1. Line 3
  2. Line 4
  3. Line 5

(几乎相同的数据,但行号已修改)

(simply the same data but with the line numbers modified)

如果您将这两种输出机制都保留在适当的位置,则输出将是

if you leave both of these output mechanisms in place, the output will be

  1. 第3行
  2. 第4行
  3. 第5行
  4. 第3行
  5. 第4行
  6. 第5行
  1. Line 3
  2. Line 4
  3. Line 5
  4. Line 3
  5. Line 4
  6. Line 5

即第一个输出与第二个输出串联.

that is, the first output concatenated with the second.

这篇关于需要在批处理文件中从a行读取到b行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆