【问题标题】:Skip line with a specific string from a file - Batch script使用文件中的特定字符串跳过行 - 批处理脚本
【发布时间】:2015-09-24 14:31:13
【问题描述】:

我正在尝试使用 Windows 批处理脚本将 abc.txt 的一部分复制到 def.txt 文件。

abc.txt

Connected to: Oracle Database 11g Enterprise Edition Release 
11.2.0.4.0 - 64bit Production. With the Partitioning, Automatic Storage
Management, OLAP, Data Mining and Real Application Testing options.

About to export specified tables via Conventional Path ...
... exporting table             table_1      2000 rows exported
..... exporting table             table_2        456512 rows exported
. . exporting table             table_3   So on...
Export successful with warnings. table_1 has some issue.

def.txt

Connected to: Oracle Database 11g Enterprise Edition Release 
11.2.0.4.0 - 64bit Production. With the Partitioning, Automatic Storage
Management, OLAP, Data Mining and Real Application Testing options.

About to export specified tables via Conventional Path ...
Export successful with warnings. table_1 has some issue.

简而言之,我想跳过包含字符串“exporting”的行并将结果移动到 def.txt。我尝试使用for /F,但无法做到这一点。能否请你帮忙。提前致谢。

【问题讨论】:

  • 感谢您的回答。 findstr /v 非常适合我。但是,如果我想单独删除点“...”,而不管 abc.txt 中的点数如何。有办法吗?

标签: file batch-file for-loop


【解决方案1】:
findstr /v /c:"exporting" abc.txt > def.txt

无需遍历文件。只需过滤输入文件中与 (/v) 包含指示文字的条件不匹配的行,并将输出发送到指示文件。

编辑以适应 cmets

@echo off
    setlocal enableextensions disabledelayedexpansion
    > def.txt (
        for /f "tokens=1,* delims=:. " %%a in ('
            findstr /n "^" abc.txt
        ') do echo(%%b
    )

这里的文件由findstr处理以对行进行编号(以保留源文件中的空行),输出由for /f命令处理,delimstokens子句配置为删除起始空格/输出行中的点/冒号。

【讨论】:

  • 感谢您的回答。 findstr /v 非常适合我。但是,如果我想单独删除点“...”,而不管 abc.txt 中的点数如何。有办法吗?
  • @SwaroopNarasimha,如果您只想删除点,那么您将需要迭代文件。查看更新的答案。
【解决方案2】:

这与 MC ND 的解决方案实际上并没有什么不同 - 但它是另一种选择:目前它区分大小写,但有一个 /i 开关来改变它。

find /v "exporting" < abc.txt > def.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多