BAT文件读取并复制从文本文件到另一个文本文件的底部16行?

时间:2009-10-27 17:10:04

标签: batch-file

我需要将底部16行从文本文件复制到另一个文本文件。我需要为所有客户端执行此过程。在客户端的位置,文本文件是常见的,但是底部16行对于确认包安装很重要。

3 个答案:

答案 0 :(得分:6)

more command可用于提取最后n行:

  1. 如果文件someFile.txt包含2000行,则可以使用(“/ E + n:开始显示第n行的第一个文件”提取最后16行) :

    more /e +1984 someFile.txt > lastLines.txt
    
  2. someFile.txt中的行数可以是:

    for /f %%i in ('find /v /c "" ^< someFile.txt') do set /a lines=%%i
    
  3. 然后more的调用变为(仍为此示例,最后16行):

    set /a startLine=%lines% - 16
    more /e +%startLine% someFile.txt > lastLines.txt
    

答案 1 :(得分:1)

您可以下载大多数Unix命令的DOS端口(例如here - 选择您喜欢的任何一组命令,包括tail

下载后,只需使用tail -16 filename.txt

即可

好处(抵消下载/解包的工作量)是你可以获得一整套非常好的Unix命令行工具。

答案 2 :(得分:0)

我调整了这个有用的代码,将51个文件附加在一起并保留第一个文件的12行标题,如下所示:

REM Append 51 files and retain 12 line header of first file
REM  ------------------------------------------------------

REM Set number of files to combine    
set Nmbrfls=51   

REM copy the first file with the header 
copy file_1.txt combined.txt

REM Loop through the other 50 files (start at #2) appending to the combined
REM file using a temporary file to capture all but the the 12 header lines
REM than append the temporary file to the combined on each loop

for /l %%i in (2,1,%Nmbrfls%) do (
more /e +13 file_%%i.txt > temp.txt
copy /b combined.txt + temp.txt combined.txt
del temp.txt
)
相关问题