使用bat文件拆分txt

时间:2013-10-26 07:01:37

标签: batch-file

我正在尝试将一系列txt文件拆分为2个。以下是其中一个文件。第一个新文件应包含“良好许可证修订”之前的所有内容。在此之后的第二个文件,包括此字符串。

我想学习的下一个技巧是如何修复长度将生成的文件分隔为csv。

提前致谢。

文件示例:

                              ENERGY RESOURCES CONSERVATION BOARD                                   

                               WELL LICENCES ISSUED  DAILY LIST                                     



DATE: 04 January 2010                                                                               


--------------------------------------------------------------------------------------------        
WELL NAME               LICENCE NUMBER         MINERAL RIGHTS       GROUND ELEVATION                
UNIQUE IDENTIFIER       SURFACE CO-ORDINATES   BOARD FIELD CENTRE   PROJECTED DEPTH                 
LAHEE CLASSIFICATION    FIELD                                       TERMINATING ZONE                
DRILLING OPERATION      WELL PURPOSE           WELL  TYPE           SUBSTANCE                       
LICENSEE                                                            SURFACE LOCATION                
--------------------------------------------------------------------------------------------        

SUNCOR ET AL MEDHAT 9-17-15-4        0416613   ALBERTA CROWN        753.3M                          
100/09-17-015-04W4/00  S  543.4M  W  167.6M    MEDICINE HAT         656.8M                        
DEV (NC)                             MEDICINE HAT                   FISH SCALE ZONE                 
VERTICAL                             NEW       PRODUCTION           GAS                             
SUNCOR ENERGY INC.                  

AMENDMENTS OF WELL LICENCES
---------------------------

WELL NAME AND U.I.D.                LICENCE NO.  AMENDED TO READ
--------------------                -----------  ---------------

PARA ET AL HZ RE VALHALLA 4-34-75-11  0358541    TARGET SUBSTANCE(1)                                
100/04-34-075-11W6/02                            GAS FROM MONTNEY FM                                

SHPACIFIC OV MCKAY 2-17-91-14         0416002    TERMINATING ZONE:                                  
1AA/02-17-091-14W4/00                            WOODBEND GRP                                       

WELL LICENCES CANCELLED
-----------------------

WELL NAME AND U.I.D.                LICENCE NO.                                                     
--------------------                -----------                                                     

QRCI ET AL THREEHCK 14-19-36-26       0404101                                                       
100/14-19-036-26W4/00                                                                               

ARC ETAL 102 PEMBINA 15-34-48-8       0407157                                                       
100/15-34-048-08W5/00                                                                               


-------------------- END OF WELL LICENCES DAILY LIST  -------------------- 

2 个答案:

答案 0 :(得分:3)

纯批次

@ECHO OFF &SETLOCAL
for /f "delims=[]" %%a in ('^<file.txt find /n "AMENDMENTS OF WELL LICENCES"') do set "LineFound=%%a"
echo "AMENDMENTS OF WELL LICENCES" found in line #%LineFound%.
set /a LineFound-=1
SETLOCAL ENABLEDELAYEDEXPANSION
<file.txt (
for /l %%a in (1,1,%LineFound%) do (
    set "Line="
    set/p "Line="
    echo(!Line!
))>file1.txt
endlocal
<file>file2.txt more +%LineFound%
type file?.txt

答案 1 :(得分:2)

这是一种强大的方法,无需加载额外的工具,并使用名为findrepl.bat的辅助批处理文件 - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

findrepl.bat放在与批处理文件相同的文件夹中。

输入文件为file.txt

type file.txt | findrepl    "." /e:"AMENDMENTS OF WELL LICENCES"  /o:-1:-1 >file1.txt
type file.txt | findrepl /v "." /e:"AMENDMENTS OF WELL LICENCES"  /o:-1:-1 >file2.txt