string 通过批处理文件删除txt文件中的某些行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/418916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:21:33  来源:igfitidea点击:

Delete certain lines in a txt file via a batch file

stringbatch-file

提问by chrome

I have a generated txt file. This file has certain lines that are superfluous, and need to be removed. Each line that requires removal has one of two string in the line; "ERROR" or "REFERENCE". These tokens may appear anywhere in the line. I would like to delete these lines, while retaining all other lines.

我有一个生成的txt文件。这个文件有一些多余的行,需要删除。每行需要删除的行中有两个字符串之一;“错误”或“参考”。这些标记可能出现在行中的任何位置。我想删除这些行,同时保留所有其他行。

So, if the txt file looks like this:

因此,如果 txt 文件如下所示:

Good Line of data
bad line of C:\Directory\ERROR\myFile.dll
Another good line of data
bad line: REFERENCE 
Good line

I would like the file to end up like this:

我希望文件最终是这样的:

Good Line of data
Another good line of data
Good line

TIA.

TIA。

回答by paxdiablo

Use the following:

使用以下内容:

type file.txt | findstr /v ERROR | findstr /v REFERENCE

This has the advantage of using standard tools in the Windows OS, rather than having to find and install sed/awk/perl and such.

这具有在 Windows 操作系统中使用标准工具的优点,而不必查找和安装 sed/awk/perl 等。

See the following transcript for it in operation:

请参阅以下文字记录以了解它的运行情况:

C:\>type file.txt
Good Line of data
bad line of C:\Directory\ERROR\myFile.dll
Another good line of data
bad line: REFERENCE
Good line

C:\>type file.txt | findstr /v ERROR | findstr /v REFERENCE
Good Line of data
Another good line of data
Good line

回答by Rick

You can accomplish the same solution as @paxdiablo's using just findstr by itself. There's no need to pipe multiple commands together:

您可以单独使用 findstr 来完成与@paxdiablo 相同的解决方案。无需将多个命令通过管道连接在一起:

findstr /V "ERROR REFERENCE" infile.txt > outfile.txt

Details of how this works:

这是如何工作的详细信息:

  • /v finds lines that don't match the search string (same switch @paxdiablo uses)
  • if the search string is in quotes, it performs an OR search, using each word (separator is a space)
  • findstr can take an input file, you don't need to feed it the text using the "type" command
  • "> outfile.txt" will send the results to the file outfile.txt instead printing them to your console. (Note that it will overwrite the file if it exists. Use ">> outfile.txt" instead if you want to append.)
  • You might also consider adding the /i switch to do a case-insensitive match.
  • /v 查找与搜索字符串不匹配的行(@paxdiablo 使用的相同开关)
  • 如果搜索字符串在引号中,则使用每个单词执行 OR 搜索(分隔符为空格)
  • findstr 可以接受一个输入文件,您不需要使用“type”命令为其提供文本
  • ">outfile.txt" 会将结果发送到文件 outfile.txt 而不是将它们打印到您的控制台。(请注意,如果文件存在,它将覆盖文件。如果要追加,请改用“>> outfile.txt”。)
  • 您还可以考虑添加 /i 开关来进行不区分大小写的匹配。

回答by Rob

If you have sed:

如果你有 sed:

sed -e '/REFERENCE/d' -e '/ERROR/d' [FILENAME]

Where FILENAMEis the name of the text file with the good & bad lines

FILENAME带有好行和坏行的文本文件的名称在哪里

回答by mirod

If you have perl installed, then perl -i -n -e"print unless m{(ERROR|REFERENCE)}"should do the trick.

如果您安装了 perl,那么perl -i -n -e"print unless m{(ERROR|REFERENCE)}"应该可以解决问题。