【问题标题】:BAT file to read and copy bottom 16 lines from a text file to another one?BAT文件读取文本文件的底部16行并将其复制到另一个文件?
【发布时间】:2009-10-27 17:10:03
【问题描述】:

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

【问题讨论】:

标签: batch-file


【解决方案1】:

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

  1. 如果文件 someFile.txt 包含 2000 行,则可以使用 ("/E +n: Start display the first file at line n") 提取最后 16 行: p>

    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
    

【讨论】:

    【解决方案2】:

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

    下载后直接使用tail -16 filename.txt

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

    【讨论】:

      【解决方案3】:

      我修改了这个有用的代码,将 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
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-29
        • 2013-05-02
        • 1970-01-01
        • 2018-06-27
        相关资源
        最近更新 更多