0

我有一系列试图进入数据库的 .txt 文件。

第 7 行“包含”破折号和空格的分隔。像 excel 这样的工具会根据整个文件尝试和空格分隔。但第 7 行之后的行在字段内有空格。

从一篇文章中,我发现我设法创建了一个 cmd.bat 脚本来删除前 6 行和页脚。现在我需要关于如何使用 row7“分隔”文件的建议。之后删除 row7 将是一个奖励。4 个字段的长度波动,因此固定字段长度不是选项。将不胜感激任何帮助。

Report Number: ST-49                                                                            ENERGY RESOURCES CONSERVATION BOARD                                                                                    Page 1 of 1
Run Date: 01 Jan 2012                                                                              DAILY DRILLING ACTIVITY LIST                                                                                                   
                                                                                      For the Notification Period 31 Dec 2011 To 01 Jan 2012                                                                                      

      WELL ID                    WELL NAME              LICENCE  +----------------CONTRACTOR-----------------+  RIG        ACTIVITY DATE       FIELD CENTRE  BA ID               LICENSEE                NEW PROJECTED  ACTIVITY  
                                                                 BA ID                   NAME                  NUMBER                                                                                     TOTAL DEPTH     TYPE    
------------------- ----------------------------------- -------- ------ -------------------------------------- ------ ----------------------- -------------- ----- ------------------------------------- ------------- -----------
00/04-11-028-28W4/0 ECA CROSSE 4-11-28-28                0438271 0Y52   Precision Drilling Corporation            239 31 Dec 2011 10:30:00 PM Midnapore      0026  EnCana Corporation                                  Drill To LD
00/10-21-038-24W4/0 ECA HAYNES 10-21-38-24               0439238 0Y52   Precision Drilling Corporation            154 31 Dec 2011 11:00:00 PM Red Deer       0026  EnCana Corporation                                  Drill To LD
00/04-08-049-18W5/0 HUSKY MINEHEAD 4-8-49-18             0440874 0Y52   Precision Drilling Corporation            401 01 Jan 2012 01:00:00 PM Drayton Valley 0R46  Husky Oil Operations Limited                        Drill To LD


                                                                            TOTAL  - Drill To LD - Drilling to Licensed Depth                     = 15                                                                            
                                                                            TOTAL  - Re-entry    - Re-entry of an Abandoned Well                  =  0                                                                            
                                                                            TOTAL  - Resumption  - Resumption of Drilling of a Non-abandoned Well =  0                                                                            
                                                                            TOTAL  - Set Surface - Drilling to Set Surface Casing Only            =  2                                                                            

                                                                                                TOTAL NUMBER OF WELLS LISTED = 17                                                                                                 
                                                                                                      *** END OF REPORT ***                                                                                                       
4

2 回答 2

2

试试这个(它可能很慢,没有改变):

@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "infile=file"
SET "outfile=out"
FOR /f %%a in ('^<"%infile%" find /c /v ""') do SET /a lline=%%a-9
FOR /f "usebackqskip=6delims=" %%a IN ("%infile%") DO IF NOT DEFINED line SET "line=%%a"
FOR %%a IN (%line%) DO (
    SET /a counter+=1
    FOR /f "delims=:" %%b IN ('(echo(%%~a^&echo(^)^|findstr /o $') DO SET /a $!counter!=%%b-2
)

(FOR /f "skip=6delims=" %%a IN ('findstr /n $ "%infile%"') DO IF %lline% gtr !cline! (
    SET "line=%%a"
    FOR /f "delims=:" %%b IN ("!line!") DO SET /a cline=%%b
    SET "line=!line:*:=!"
    SET /a cstart=0
    SET "Newline="
    FOR /l %%b IN (1,1,%counter%) DO (
        CALL SET "token=%%line:~!cstart!,!$%%b!%%"
        SET "flag=true"
        SET /a tstop=!$%%b!-1
        FOR /l %%c IN (!tstop!,-1,0) DO IF DEFINED flag IF NOT "!token:~%%c,1!"==" " (
            SET /a tstop=%%c+1
            FOR /f %%d IN ("!tstop!") DO SET "token=!token:~0,%%d!"
            FOR /f "tokens=*" %%d IN ("!token!") DO SET "token=%%d"
            SET "flag="
        ) ELSE (
            IF %%c equ 0 SET "token="
        )
        IF %%b equ 1 (SET "Newline="!token!"") ELSE SET "Newline=!Newline!,"!token!""
        SET /a cstart+=!$%%b!
    )
    ECHO(!Newline!
))>"%outfile%"
TYPE "%outfile%"

..输出:

"00/04-11-028-28W4/0","ECA CROSSE 4-11-28-28","0438271","0Y52","Precision Drilling Corporation","239","31 Dec 2011 10:30:00 PM","Midnapore","0026","EnCana Corporation","","Drill To LD"
"00/10-21-038-24W4/0","ECA HAYNES 10-21-38-24","0439238","0Y52","Precision Drilling Corporation","154","31 Dec 2011 11:00:00 PM","Red Deer","0026","EnCana Corporation","","Drill To LD"
"00/04-08-049-18W5/0","HUSKY MINEHEAD 4-8-49-18","0440874","0Y52","Precision Drilling Corporation","401","01 Jan 2012 01:00:00 PM","Drayton Valley","0R46","Husky Oil Operations Limited","","Drill To LD"

这不适^"!%*=~用于 infile 中的特殊字符。

您最好在 Windows 上使用 awkFIELDWIDTHSBEGIN模式中设置。您可以将它与sed for Windows结合使用。

于 2013-09-02T09:19:38.690 回答
0

尝试使用限定符,即“或任何可以使您的应用程序或工具识别该列从这里开始的东西。它会忽略列中的分隔符。

于 2013-09-02T06:47:37.147 回答