Delete specific lines in txt file via batch file

I have a generated txt file. This file has certain lines that are redundant and need to be deleted. Each row requiring deletion has one row per row; ERROR or LINK. These tokens can appear anywhere on the line. I would like to delete these lines while keeping all the 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 complete as follows:

 Good line of data
 Another good line of data
 Good line

TIA.

+55
string batch-file
Jan 07 '09 at 1:59
source share
4 answers

Use the following:

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

This has the advantage of using standard tools on Windows, rather than finding and installing sed / awk / perl, etc.

See the following protocol for 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
+82
Jan 07 '09 at 2:12
source share

You can execute the same solution as @paxdiablo using only findstr. There is no need to link several teams together:

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

Details on how this works:

  • / v finds strings that don't match the search string (the same @paxdiablo switch uses)
  • If the search string is in quotation marks, it performs an OR search using each word (the delimiter is a space)
  • findstr can accept an input file, you do not need to feed it with text using the "type" command
  • "> outfile.txt" will send the results to the outfile.txt file and then print them to the console. (Note that it will overwrite the file if it exists. Instead, use "→ outfile.txt" if you want to add.)
  • You might also consider adding an / i switch to match case insensitivity.
+30
Apr 18 '13 at 18:47
source share

If you have sed:

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

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

+5
Jan 7 '09 at 2:10
source share

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

+1
Jan 07 '09 at 2:12
source share



All Articles