Batch file 从标准输出中提取一部分输出

Batch file 从标准输出中提取一部分输出,batch-file,Batch File,今天我做了大约第五次for/?,并在所有屏幕上划出一段距离,以便找到替换参考的底部。之后,我决定想出一种快速简便的方法。过了一会儿,我想到了: for /? | more +118 我将其转储到一个批处理文件中,并将其命名为optparams.cmd,它完全符合我的要求。现在,我想对SET做同样的事情,但只列出关于环境变量替换的部分。我可以再做一次更多的把戏,但完成后我必须滚动其他屏幕 我的问题是:在给定开始和结束参数的情况下,最简单的方法是什么?行号或某些文本字符串最好。我想把所有的数据都

今天我做了大约第五次
for/?
,并在所有屏幕上划出一段距离,以便找到替换参考的底部。之后,我决定想出一种快速简便的方法。过了一会儿,我想到了:

for /? | more +118 
我将其转储到一个批处理文件中,并将其命名为optparams.cmd,它完全符合我的要求。现在,我想对
SET
做同样的事情,但只列出关于环境变量替换的部分。我可以再做一次
更多的
把戏,但完成后我必须滚动其他屏幕

我的问题是:在给定开始和结束参数的情况下,最简单的方法是什么?行号或某些文本字符串最好。我想把所有的数据都读入一个数组是一个开始,但我现在仍在尝试批量处理数组。任何洞察或提示都将不胜感激

编辑:我把MC ND的帖子变成了下面的函数,基本上实现了我想要的功能

    call :ExtractBetween 68 96 "set /?"
    exit /b


    :ExtractBetween start stop cmd
    @echo off & setlocal
    set _start=%1 & set _end=%2 & set _cmd=%~3
    for /F "tokens=1,* delims=:" %%f in (
    '%_cmd% ^| findstr /n /r "." ') do (
        if %%f geq %_start% if %%f leq %_end% (
            echo %%g)
    ) 
    exit /b

使用findstr计算行数,并在行号之间进行过滤

@echo off
    setlocal enableextensions
    set _start=%1
    set _end=%2
    for /F "tokens=1,* delims=:" %%f in ('for /? ^| findstr /n /r "." ') do (
        if %%f geq %_start% if %%f leq %_end% (
            echo %%g
        )
    )

使用findstr计算行数,并在行号之间进行过滤

@echo off
    setlocal enableextensions
    set _start=%1
    set _end=%2
    for /F "tokens=1,* delims=:" %%f in ('for /? ^| findstr /n /r "." ') do (
        if %%f geq %_start% if %%f leq %_end% (
            echo %%g
        )
    )
具有和范围地址的示例:

>set /? | sed "/Environment variable substitution/,+24!d Environment variable substitution has been enhanced as follows: %PATH:str1=str2% would expand the PATH environment variable, substituting each occurrence of "str1" in the expanded result with "str2". "str2" can be the empty string to effectively delete all occurrences of "str1" from the expanded output. "str1" can begin with an asterisk, in which case it will match everything from the beginning of the expanded output to the first occurrence of the remaining portion of str1. May also specify substrings for an expansion. %PATH:~10,5% would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result. If the length is not specified, then it defaults to the remainder of the variable value. If either number (offset or length) is negative, then the number used is the length of the environment variable value added to the offset or length specified. %PATH:~-10% would extract the last 10 characters of the PATH variable. >set /? | sed "/Environment variable substitution/,/would extract/!d Environment variable substitution has been enhanced as follows: %PATH:str1=str2% would expand the PATH environment variable, substituting each occurrence of "str1" in the expanded result with "str2". "str2" can be the empty string to effectively delete all occurrences of "str1" from the expanded output. "str1" can begin with an asterisk, in which case it will match everything from the beginning of the expanded output to the first occurrence of the remaining portion of str1. May also specify substrings for an expansion. %PATH:~10,5% would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result. If the length is not specified, then it defaults to the remainder of the variable value. If either number (offset or length) is negative, then the number used is the length of the environment variable value added to the offset or length specified. %PATH:~-10% would extract the last 10 characters of the PATH variable. >设置/?|sed“/环境变量替换/,+24!d 环境变量替换已增强,如下所示: %路径:str1=str2% 将展开PATH环境变量,替换每个引用 扩展结果中的“str1”的“str2”。“str2”可以为空 字符串,以有效地从扩展列表中删除所有出现的“str1” 输出。“str1”可以以星号开头,在这种情况下,它将匹配 从扩展输出的开始到第一个输出 str1剩余部分的出现。 也可以为展开指定子字符串。 %路径:~10,5% 将展开PATH环境变量,然后仅使用5 以展开字符的第11个(偏移量10)字符开始的字符 结果。如果未指定长度,则默认为 变量值的剩余部分。如果数字(偏移量或长度)为 负数,则使用的数字是环境变量的长度 添加到指定偏移量或长度的值。 %路径:~-10% 将提取PATH变量的最后10个字符。 >set/?| sed)/环境变量替换/,/将提取/!D 环境变量替换已增强,如下所示: %路径:str1=str2% 将展开PATH环境变量,替换每个引用 在扩展结果中使用“str2”替换“str1”。“str2”可以是空的 字符串,以有效地从扩展列表中删除所有出现的“str1” 输出“str1”可以以星号开头,在这种情况下,它将匹配 从扩展输出的开始到第一个输出 str1剩余部分的出现。 也可以为展开指定子字符串。 %路径:~10,5% 将展开PATH环境变量,然后仅使用5 以展开字符的第11个(偏移量10)字符开始的字符 后果如果未指定长度,则默认为 变量值的剩余部分。如果任意一个数字(偏移量或长度)为 负数,则使用的数字是环境变量的长度 添加到指定偏移量或长度的值。 %路径:~-10% 将提取PATH变量的最后10个字符。 具有和范围地址的示例:

>set /? | sed "/Environment variable substitution/,+24!d Environment variable substitution has been enhanced as follows: %PATH:str1=str2% would expand the PATH environment variable, substituting each occurrence of "str1" in the expanded result with "str2". "str2" can be the empty string to effectively delete all occurrences of "str1" from the expanded output. "str1" can begin with an asterisk, in which case it will match everything from the beginning of the expanded output to the first occurrence of the remaining portion of str1. May also specify substrings for an expansion. %PATH:~10,5% would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result. If the length is not specified, then it defaults to the remainder of the variable value. If either number (offset or length) is negative, then the number used is the length of the environment variable value added to the offset or length specified. %PATH:~-10% would extract the last 10 characters of the PATH variable. >set /? | sed "/Environment variable substitution/,/would extract/!d Environment variable substitution has been enhanced as follows: %PATH:str1=str2% would expand the PATH environment variable, substituting each occurrence of "str1" in the expanded result with "str2". "str2" can be the empty string to effectively delete all occurrences of "str1" from the expanded output. "str1" can begin with an asterisk, in which case it will match everything from the beginning of the expanded output to the first occurrence of the remaining portion of str1. May also specify substrings for an expansion. %PATH:~10,5% would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result. If the length is not specified, then it defaults to the remainder of the variable value. If either number (offset or length) is negative, then the number used is the length of the environment variable value added to the offset or length specified. %PATH:~-10% would extract the last 10 characters of the PATH variable. >设置/?|sed“/环境变量替换/,+24!d 环境变量替换已增强,如下所示: %路径:str1=str2% 将展开PATH环境变量,替换每个引用 扩展结果中的“str1”的“str2”。“str2”可以为空 字符串,以有效地从扩展列表中删除所有出现的“str1” 输出。“str1”可以以星号开头,在这种情况下,它将匹配 从扩展输出的开始到第一个输出 str1剩余部分的出现。 也可以为展开指定子字符串。 %路径:~10,5% 将展开PATH环境变量,然后仅使用5 以展开字符的第11个(偏移量10)字符开始的字符 结果。如果未指定长度,则默认为 变量值的剩余部分。如果数字(偏移量或长度)为 负数,则使用的数字是环境变量的长度 添加到指定偏移量或长度的值。 %路径:~-10% 将提取PATH变量的最后10个字符。 >set/?| sed)/环境变量替换/,/将提取/!D 环境变量替换已增强,如下所示: %路径:str1=str2% 将展开PATH环境变量,替换每个引用 在扩展结果中使用“str2”替换“str1”。“str2”可以是空的 字符串,以有效地从扩展列表中删除所有出现的“str1” 输出“str1”可以以星号开头,在这种情况下,它将匹配 从扩展输出的开始到第一个输出 str1剩余部分的出现。 也可以为展开指定子字符串。 %路径:~10,5% 将展开PATH环境变量,然后仅使用5 以展开字符的第11个(偏移量10)字符开始的字符 后果如果未指定长度,则默认为 变量值的剩余部分。如果任意一个数字(偏移量或长度)为 负数,则使用的数字是环境变量的长度 添加到指定偏移量或长度的值。 %路径:~-10% 将提取PATH变量的最后10个字符。
下面的批处理文件提取一系列行:从第一个字符串的第一次出现到第二个字符串的最后一次出现,或多行:

@echo off
setlocal DisableDelayedExpansion

if "%~3" neq "" goto start
echo Show a range of lines, from "start string" to "end string" or number of lines
echo/
echo ShowRange.bat inputfile "start string" "end string"
echo ShowRange.bat inputfile "start string" /N:24
goto :EOF

:start
set end=%~3
if /I "%end:~0,3%" neq "/N:" (
   for /F "delims=:" %%a in ('findstr /N /C:%2 /C:%3 %1') do (
      if not defined start (
         set /A start=%%a-1
      ) else (
         set end=%%a
      )
   )
   set /A lines=end-start
) else (
   for /F "delims=:" %%a in ('findstr /N /C:%2 %1') do (
      if not defined start set /A start=%%a-1
   )
   set lines=%end:~3%
)

if %start% neq 0 set skip=skip=%start%
for /F "%skip% delims=" %%a in ('findstr /N "^" %1') do (
   set "line=%%a"
   setlocal EnableDelayedExpansion
   set "line=!line:*:=!"
   echo(!line!
   set /A lines-=1
   if "!lines!" equ "0" goto :EOF
   for /F %%b in ("!lines!") do endlocal & set lines=%%b
)
例如:

set /? > set.txt
ShowRange.bat set.txt "Environment variable substitution" "would extract"
ShowRange.bat set.txt "Environment variable substitution" /N:24
set /? | ShowRangePipe.bat "Environment variable substitution" "would extract"
set /? | ShowRangePipe.bat "Environment variable substitution" /N:24
编辑:以下版本从STDIN读取输入,以便与管道一起使用,但速度较慢,因为它必须在每个输入行中实现“查找字符串”部分:<