从STDOUT中提取输出的一部分

时间:2013-10-24 19:58:36

标签: batch-file

今天大约第五次我做了for /?并且在所有屏幕之间间隔以找到替换参考的底部我决定想出一种快速简单的方法。过了一会儿,我提出了:

for /? | more +118 

我将其转储到批处理文件中并将其称为optparams.cmd,它完全符合我的要求。现在,我想对SET执行相同的操作,但仅列出有关环境变量替换的部分。我可以再次执行MORE技巧,但是当我完成时我必须滚动浏览其余的屏幕。

我的问题是:在给定起始和结束参数的情况下,仅显示STDOUT部分的最简单方法是什么?行号或某些文本字符串最好。我正在考虑将它全部读入一个数组将是开始的方式,但我现在仍然试图批量处理数组。任何洞察或提示赞赏。

编辑:我将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

3 个答案:

答案 0 :(得分:3)

下面的批处理文件提取一系列行:从第一个字符串的第一次出现到第二个字符串的最后一次出现,或者多行:

@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

编辑:以下版本从STDIN读取输入,因此它可以与管道一起使用,但速度较慢,因为它必须与每个输入行实现“查找字符串”部分:

@echo off
setlocal DisableDelayedExpansion

if "%~2" neq "" goto start
echo Show a range of lines, from "start string" to "end string" or number of lines
echo/
echo command ^| ShowRangePipe.bat "start string" "end string"
echo command ^| ShowRangePipe.bat "start string" /N:24
goto :EOF

:start
set startFound=no
set end=%~2
if /I "%end:~0,3%" neq "/N:" (
   for /F "delims=" %%a in ('findstr /N "^"') do (
      set "line=%%a"
      setlocal EnableDelayedExpansion
      set "line=!line:*:=!"
      if "!startFound!" equ "no" (
         if defined line if "!line:%~1=!" neq "!line!" set startFound=yes & echo !line!
      ) else (
         echo(!line!
         if defined line if "!line:%~2=!" neq "!line!" goto :EOF
      )
      for /F %%b in ("!startFound!") do endlocal & set startFound=%%b
   )
) else (
   set /A lines=%end:~3%-1
   for /F "delims=" %%a in ('findstr /N "^"') do (
      set "line=%%a"
      setlocal EnableDelayedExpansion
      set "line=!line:*:=!"
      if "!startFound!" equ "no" (
         if defined line if "!line:%~1=!" neq "!line!" set startFound=yes & echo !line!
      ) else (
         echo(!line!
         set /A lines-=1
         if "!lines!" equ "0" goto :EOF
      )
      for /F "tokens=1,2" %%b in ("!startFound! !lines!") do endlocal & set "startFound=%%b" & set lines=%%c
   )
)

例如:

set /? | ShowRangePipe.bat "Environment variable substitution" "would extract"
set /? | ShowRangePipe.bat "Environment variable substitution" /N:24

答案 1 :(得分:2)

使用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
        )
    )

答案 2 :(得分:2)

sed for Windows和范围地址的示例:

>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.