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.
4 个解决方案
#1
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
#2
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开关以执行不区分大小写的匹配。
#3
If you have sed:
如果你有sed:
sed -e '/REFERENCE/d' -e '/ERROR/d' [FILENAME]
sed -e'/ REFERENCE / d'-e'/ ERROR / d'[FILENAME]
Where FILENAME is the name of the text file with the good & bad lines
其中FILENAME是包含好行和坏行的文本文件的名称
#4
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除非m {(ERROR | REFERENCE)}”应该这样做。
#1
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
#2
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开关以执行不区分大小写的匹配。
#3
If you have sed:
如果你有sed:
sed -e '/REFERENCE/d' -e '/ERROR/d' [FILENAME]
sed -e'/ REFERENCE / d'-e'/ ERROR / d'[FILENAME]
Where FILENAME is the name of the text file with the good & bad lines
其中FILENAME是包含好行和坏行的文本文件的名称
#4
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除非m {(ERROR | REFERENCE)}”应该这样做。