JT_man注:或者
------------------------123_2.bat-------------------------------
@echo off
setlocal enabledelayedexpansion
set "lines=3"
rem 顯示前3行
set i=-1
set "ok="
for /f "delims=" %%a in ('more ^< numbers.txt') do (
set /a i+=1
if !i!==%lines% set ok=1
if not defined ok echo %%a
)
pause>nul
------------------------123_2.bat-------------------------------
>>Displaying the last line,This example prints ten.
顯示最后一行,打印第十行的例子如下:
------------------------10.bat--------------------------------------------
@echo off & setlocal ENABLEEXTENSIONS
for /f "delims=" %%a in ('more ^< numbers.txt') do set "last=%%a"
echo/%last%
pause>nul
------------------------10.bat--------------------------------------------
>>Displaying the last X lines,This example prints nine and ten.
顯示最后x行,打印第9,10行的例子如下:
------------------------lastx.bat-----------------------------------------------
@echo off & setlocal ENABLEEXTENSIONS
set "lines=2"
for /f %%a in ('find/c /v "" ^< numbers.txt') do set/a skip=%%a-lines
for /f "delims=" %%a in ('more/e +%skip% ^< numbers.txt') do (
:: 或者 for /f "skip=%skip% delims=" %%a in ('more ^< numbers.txt') do (
echo/%%a
)
pause>nul
------------------------lastx.bat------------------------------------------------
>>Displaying the Nth line,This example prints three.
Note that instead of using the more command's /e switch, the skip option could have been used with the for /f command, however, this fails is it is set to any number less than one.
顯示第n行,打印第三行.
注意啟用more命令的擴展功能(/e)開(kāi)關(guān),同時(shí),ship選項過(guò)去常用于for /f命名中,但是當設定的數目小于實(shí)際值時(shí),將導致失敗
------------------------Nth.bat-----------------------------------------------
@echo off & setlocal ENABLEEXTENSIONS
set LineNo=3
set "line="
set/a LineNo-=1
for /f "delims=" %%a in ('more/e +%LineNo% ^< numbers.txt') do (
if not defined line set "line=%%a"
)
echo/%line%
pause>nul
------------------------Nth.bat------------------------------------------------
>>Displaying the Nth line plus X number of lines,This example prints five and six.
顯示第n+x行,打印第5,6行的例子如下:
------------------------x+Nth.bat--------------------------------------------------
@echo off & setlocal ENABLEEXTENSIONS
set start=5
set "lines=2"
set/a i=-1,start-=1
set "ok="
for /f "delims=" %%a in ('more/e +%start% ^< numbers.txt') do (
set/a i+=1 & for /f %%z in ('echo/%%i%%') do (
if "%%z"=="%lines%" set ok=1
)
if not defined ok echo/%%a
)
pause>nul
------------------------x+Nth.bat----------------------------------------------------