如何在Windows CMD中使用'for'命令剪切不需要的文本?

时间:2017-07-09 18:52:12

标签: batch-file cmd

我有一个文本文件,如下所示:

@X ABCDEFGHIJKLM_NOPQRST Y=66 XN=80 XC=132 FONTSIZE=4.0 FONTSIZECOND=3.0 ML=0.0 MR=0.0 MT=0.0 MB=0.0 @x@R111,2222222,222227,22222222,0,"33333333333333333333333333","444444444444444444444444","AAAAAAAAAA BBBBBBB CCCCCCCCCCC D.D||EEEEEEEEE 11|11-111EEEEEEE","FFFFFF GGGGGGGGGGG||HHHHHH|IIIIIII",0,11111111,"HHHHHH II JJ 00/000|||","","","99"
111,2222222,222227,22222222,0,"33333333333333333333333333","444444444444444444444444","AAAAAAAAAA BBBBBBB CCCCCCCCCCC D.D||EEEEEEEEE 11|11-111EEEEEEE","FFFFFF GGGGGGGGGGG||HHHHHH|IIIIIII",0,11111111,"HHHHHH II JJ 00/000|||","","","99"
111,2222222,222227,22222222,0,"33333333333333333333333333","444444444444444444444444","AAAAAAAAAA BBBBBBB CCCCCCCCCCC D.D||EEEEEEEEE 11|11-111EEEEEEE","FFFFFF GGGGGGGGGGG||HHHHHH|IIIIIII",0,11111111,"HHHHHH II JJ 00/000|||","","","99"

我需要删除不需要的文字,只留下所需的内容。 最后,我应该得到一个新的文本文件,如下所示:

111,2222222,222227,22222222,0,"33333333333333333333333333","444444444444444444444444","AAAAAAAAAA BBBBBBB CCCCCCCCCCC D.D||EEEEEEEEE 11|11-111EEEEEEE","FFFFFF GGGGGGGGGGG||HHHHHH|IIIIIII",0,11111111,"HHHHHH II JJ 00/000|||","","","99"
111,2222222,222227,22222222,0,"33333333333333333333333333","444444444444444444444444","AAAAAAAAAA BBBBBBB CCCCCCCCCCC D.D||EEEEEEEEE 11|11-111EEEEEEE","FFFFFF GGGGGGGGGGG||HHHHHH|IIIIIII",0,11111111,"HHHHHH II JJ 00/000|||","","","99"
111,2222222,222227,22222222,0,"33333333333333333333333333","444444444444444444444444","AAAAAAAAAA BBBBBBB CCCCCCCCCCC D.D||EEEEEEEEE 11|11-111EEEEEEE","FFFFFF GGGGGGGGGGG||HHHHHH|IIIIIII",0,11111111,"HHHHHH II JJ 00/000|||","","","99"

我想通过Windows中的纯CMD来做到这一点。我不想使用任何其他软件。我需要留下的文字(每一行)有什么好处是一个正则表达式,如下所示:任意数字的位数,任意数字的数字,任意数字的任意数字,“任意数字的数字”

我知道这里唯一的解决方案是命令'for',但我自己无法处理这个问题:( 任何人都可以帮我写一个批处理文件来完成这项工作吗?

此致 彼得

2 个答案:

答案 0 :(得分:0)

这适用于您的样本数据,但取决于" @ x @ R"结束不需要的文本部分。目前还不清楚是否存在,但如果没有,您可以根据需要进行调整。

它实际上取决于@ x @ R中的第一个@,然后有3个以下字符可以是任何东西。在您的样本数据中,它们是" x @ R"。

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

for /f "tokens=1,* delims=@" %%a in (Test.txt) do (
    if [%%b]==[] (
        rem Didn't have an @ so use the line as is
        echo else %%a
    ) else (
        rem Did have an @ so get the part following the @
        set Text=%%b

        rem then get the rest of the string beginning at the first character past the x@R
        echo first !Text:~3!
    )
    rem Blank line between outputs
    echo.
)
ENDLOCAL
pause
exit /b

答案 1 :(得分:0)

根据thx1138v2的回答,我做了以下的事情:

@echo off    
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "tokens=1,* delims=@" %%a in (input.txt) do (
if [%%b]==[] (
    set var1=%%a
    for /f "tokens=1-15 delims=," %%a in ("!var1!") do (
    set line=%%a,%%b,%%c,%%d,%%e,%%f,%%g,%%h,%%i,%%j,%%k,%%l,%%m,%%n
    set end=%%o
    echo !line!,!end:~0,4! >> output.txt
    )
        ) else (
    set Text=%%b
    echo !Text:~3! >> output.txt
    )
)
endlocal
pause
exit /b

我知道我是以一种非常原始的方式做到的,但它正在做我想做的事情,所以它对我来说已经足够了。

非常感谢您的帮助。