Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete certain lines in a txt file via a batch file

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.

like image 242
chrome Avatar asked Jan 07 '09 01:01

chrome


People also ask

How do I delete a specific line?

Delete lines or connectorsClick the line, connector, or shape that you want to delete, and then press Delete. Tip: If you want to delete multiple lines or connectors, select the first line, press and hold Ctrl while you select the other lines, and then press Delete.

What is %% in a batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


1 Answers

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 
like image 110
paxdiablo Avatar answered Sep 19 '22 14:09

paxdiablo