批量删除或跳过txt的最后一行

时间:2016-11-02 14:33:33

标签: batch-file

我需要删除或跳过txt的最后一行, 这是我的txt

cod_est nro_prsna_clnte          cod_prdto                cod_sub_prdto            cod_cntto                fch_est                
------- ------------------------ ------------------------ ------------------------ ------------------------ -----------------------
C                      777388304                       93                     2003             690005635930 2016-10-17 00:00:00.000

A                   910065094290                       93                     2003             690018602410 2016-10-17 00:00:00.000

A                   910065094300                       93                     2003             690018602420 2016-10-17 00:00:00.000

A                   910065094280                       93                     2003             690018602400 2016-10-17 00:00:00.000

(4 rows affected)

,批次就是这个

for /F "tokens=1-6 skip=2" %%i in (OFB_INF_PSN_2.txt) do (echo %%i,%%j,%%k,%%l,%%m,%%n>> OFB_INF_PSN.txt)

结果就是这个

C,777388304,93,2003,690005635930,2016-10-17

A,910065094290,93,2003,690018602410,2016-10-17

A,910065094300,93,2003,690018602420,2016-10-17

A,910065094280,93,2003,690018602400,2016-10-17

(4,rows,affected),,,

但我需要删除或跳过最后一行((4,行,受影响),,,)

任何人都可以帮助我 非常感谢你

4 个答案:

答案 0 :(得分:1)

粗略地说,既然您提供的数据不太可能缺少*最后想要的列,您可以使用

 for /F "tokens=1-6 skip=2" %%i in (OFB_INF_PSN_2.txt) do if "%%n" neq "" (echo %%i,%%j,%%k,%%l,%%m,%%n>> OFB_INF_PSN.txt)

此处"%%n"可能"%%l%%m%%n"表示“最后3个结果列中的每一列都为空”

答案 1 :(得分:0)

您的另一个选择是将文件的输出传递给FIND命令,以查找没有“受影响的行”字样的所有行

for /F "tokens=1-6 skip=2" %%i in ('type OFB_INF_PSN_2.txt ^|find /I /V "rows affected"') do (echo %%i,%%j,%%k,%%l,%%m,%%n>> OFB_INF_PSN.txt)

答案 2 :(得分:0)

跳过最后一行独立于其内容的更一般方法是以下代码:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "FILE_IN=OFB_INF_PSN_2.txt"
set "FILE_OUT=OFB_INF_PSN.txt"

rem // Redirect the entire output data once only:
> "%FILE_OUT%" (
    rem // Reset variable holding line string:
    set "LINE="
    rem // Read text file line by line:
    for /F "usebackq skip=2 tokens=1-6" %%I in ("%FILE_IN%") do (
        rem // Toggle delayed expansion to not lose exclamation marks:
        setlocal EnableDelayedExpansion
        rem // Avoid output of initial empty line:
        if defined LINE (
            rem // Output previous line string:
            echo(!LINE!
        )
        endlocal
        rem // Store current line string to be output during next iteration delayed:
        set "LINE=%%I,%%J,%%K,%%L,%%M,%%N"
    )
)

endlocal
exit /B

答案 3 :(得分:0)

此示例批处理文件应仅输出以数字结尾的行:

@(For /F "Tokens=1-6" %%A In ('FindStr [0-9]$ "OFB_INF_PSN_2.txt"') Do @Echo=%%A,%%B,%%C,%%D,%%E,%%F)>"OFB_INF_PSN.txt"