标题: [文本处理] 批处理怎样截取文本倒数第三行的内容? [打印本页]
作者: jwwlchen 时间: 2012-12-27 21:25 标题: 批处理怎样截取文本倒数第三行的内容?
有一系列.txt文本文件,行数不固定,怎么把每个文件的倒数第三行内容导出到一个文件中?
作者: batman 时间: 2012-12-27 21:42
- @echo off&setlocal enabledelayedexpansion
- (for %%a in (*.txt) do (
- for /f "usebackq delims=" %%b in ("%%a") do (
- set "str2=!str1!"
- set "str1=!str!"
- set "str=%%a"
- )
- if defined str2 echo,!str2!&set "str2="
- ))>new.txt
- start new.txt
复制代码
作者: batman 时间: 2012-12-27 22:04
附取文本倒数第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
复制代码
作者: BAT-VBS 时间: 2012-12-27 23:35
- @echo off
- set n=0
- for /f %%a in (a.txt) do (
- set /a n+=1
- )
- echo %n%
- set /a n-=3
- call :getLine %n%
- pause
- goto :eof
-
- :getLine
- for /f "skip=%1 delims=" %%a in (a.txt) do (
- echo,%%a
- goto :eof
- )
复制代码
作者: terse 时间: 2012-12-28 00:28
- @echo off&setlocal enabledelayedexpansion
- for /f "delims=" %%i in ('dir /b /a-d *.TXT') do (
- sort /+8192 "%%i"|more +2 >_ten.t
- set /pstr=<_ten.t
- >>_temp.txt echo;!str!
- )
- del _ten.t&pause
复制代码
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |