附取文本倒数第N行的两种方法:
1、- @echo off&setlocal enabledelayedexpansion
- (for /l %%a in (1,1,100) do echo %%a)>a.txt
- set /a n=!random!%%100+1
- for /f "delims=" %%a in (a.txt) do (
- for /l %%b in (%n%,-1,1) do (
- if %%b neq 1 (
- set /a a=%%b-1
- for %%c in (!a!) do set "str%%b=!str%%c!"
- ) else (
- set "str%%b=%%a"
- )
- )
- )
- echo 取得a.txt倒数第%n%行的值为:!str%n%!
- pause>nul
复制代码 2、- @echo off&setlocal enabledelayedexpansion
- (for /l %%a in (1,1,100) do echo %%a)>a.txt
- for /f "delims=" %%a in (a.txt) do set /a m+=1&set "_!m!=%%a"
- set /a n=!random!%%100+1,m-=n-1
- echo 取得a.txt倒数第%n%行的值为:!_%m%!
- pause>nul
复制代码
|