Delete certain lines in a txt file via a batch file

StringBatch File

String Problem Overview


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.

So, if the txt file looks like this:

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.

String Solutions


Solution 1 - String

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.

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

Solution 2 - String

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

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.

Solution 3 - String

If you have sed:

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

Where FILENAME is the name of the text file with the good & bad lines

Solution 4 - String

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

Solution 5 - String

It seems that using the FIND instead of the FINDSTR can support also unicode characters. e.g. type file.txt | find /v "Ω"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchromeView Question on Stackoverflow
Solution 1 - StringpaxdiabloView Answer on Stackoverflow
Solution 2 - StringRickView Answer on Stackoverflow
Solution 3 - StringRobView Answer on Stackoverflow
Solution 4 - StringmirodView Answer on Stackoverflow
Solution 5 - StringHary DeeView Answer on Stackoverflow