2013-10-24 38 views
1

大約今天第五次我做了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 
+0

避免這個問題,而不是滾動瀏覽其餘的屏幕,只需點擊CTRL-C(或Q即可)? ;-) –

+0

最簡單的方法是爲此採取'sed'。 – Endoro

回答

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 

EDIT:下面的版本從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 
+0

非常好,但你能不能先寫文本文件呢? ;) –

+0

是的,但在這種情況下,程序比較慢,因爲它必須搜索每一行中的字符串。我在上面加了這樣的版本... – Aacini

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

例如用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.