Bat文件从文本文件中删除数据

时间:2019-09-09 14:28:29

标签: batch-file

我有bat文件,该文件可以按预期将我的数据从一行拆分为多行,但是它也会占用不必要的数据。

txt文件中的数据- 数据将采用以下相同格式

85:85123,  ,Smith,181215:image.tif,image2.tif,image3.tif
85:85456,  ,Richard,184985:image4.tif,image5.tif
85:85789,  ,Aly,589456:image.tif

我得到的输出是:

123,
123,smith
123,image.tif
123,image2.tif
123,image3.tif
456,
456,Richard
456,image4.tif
456,image5.tif
789,
789,Aly
789,image.tif

我想要的输出:

123,image.tif
123,image2.tif
123,image3.tif
456,image4.tif
456,image5.tif
789,image.tif

我只希望每行上都有唯一的带有图像名称的编号,但是我也得到了一个带有空白值和名字的唯一编号。

@echo off

> "Output.csv" (
    for /F "usebackq tokens=1-2* delims=: " %%A in ("transact.txt") do (
        set "NUM=%%B" & set "LIST=%%C"
         setlocal EnableDelayedExpansion
        for %%D in ("!NUM:*%%A=!") do (
            for %%E in ("!LIST:,=" "!") do (
                endlocal
                echo(%%~D %%~E
                setlocal EnableDelayedExpansion
            )
        )
        endlocal
    )
)

2 个答案:

答案 0 :(得分:0)

为了获得所需的输出,只需进行3个最小的更改即可。

:: Q:\Test\2019\09\09\SO_57856028.cmd
@echo off
> "Output.csv" (
    for /F "usebackq tokens=1-4* delims=:, " %%A in ("transact.txt") do (
        set "NUM=%%B" & set "LIST=%%E"
        setlocal EnableDelayedExpansion
        for %%D in ("!NUM:*%%A=!") do (
            for %%E in ("!LIST:,=" "!") do (
                endlocal
                echo(%%~D,%%~E
                setlocal EnableDelayedExpansion
            )
        )
        endlocal
    )
)

答案 1 :(得分:0)

好吧,您似乎想过滤掉所有名称不是image的文件,后跟一个可选数字,再跟.tif,这可以通过插入一些代码来完成(请参见{ {1}}备注):

rem