【问题标题】:How do I create a batch file that reads a file and writes a substring into another file?如何创建读取文件并将子字符串写入另一个文件的批处理文件?
【发布时间】:2013-01-22 09:48:05
【问题描述】:

我目前有一个需要解析的 Clear-Case 命令导出的文本文件 (output.txt)。它看起来像这样:

Comparing the following:
  R1.PROD.V1@\VOB_pvob
  R2.PROD.V1@\VOB_pvob
Differences:
>> M:\ACME_PROD\src\ACME@@
>> M:\ACME_PROD\src\ACME\file 2.txt@@
>> M:\ACME_PROD\src\ACME\file 1.txt@@
>> M:\ACME_PROD\src\ACME\file 3.txt@@

我想做的是使用 findstr 命令过滤“>>”和“@@”字符串之间包含的字符串,以获得如下所示的输出文件(如果可能,请使用引号:

"M:\ACME_PROD\src\ACME"
"M:\ACME_PROD\src\ACME\file 2.txt"
"M:\ACME_PROD\src\ACME\file 1.txt"
"M:\ACME_PROD\src\ACME\file 3.txt"

我是编写批处理文件的新手,所以我不知道从哪里开始。我设法找到了可以循环遍历文本文件行和 findstr 命令的单独代码的代码,但是我试图将它们放在一起时遇到了困难!

最好的问候,

安德鲁

【问题讨论】:

    标签: parsing batch-file substring


    【解决方案1】:

    给你

    setlocal enabledelayedexpansion
    for /f "skip=1 tokens=* delims=> " %%a in ('"findstr /r [\w^>*] output.txt"') do (
    set line=%%a
    set line=!line:@=!
    echo "!line!" >>new.txt
    )
    

    过滤后的字符串会输出到new.txt

    【讨论】:

    • 这太棒了!我怎样才能让它只复制以 >> 开头的行而不是所有行?
    • @Andrew 对不起,我没有意识到那是在走其他路线,我已经更新了我的答案。
    猜你喜欢
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    相关资源
    最近更新 更多