tags:

views:

8158

answers:

4

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.

+2  A: 

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

Rob
A: 

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

mirod
+10  A: 

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
paxdiablo
Great, just the line I was looking for.
chrome
Had this exact problem and found it here on SO. God bless Stack Overflow!!
Matt Rogish
To be complete: you need to add "> output.txt" (without quotes) to have the output written in a file.
Burkhard
Thanks, this was quick and easy just the way I like it!
StarShip3000
@StarShip3000, I'd be _very_ surprised if piping a text file through `findstr` would _modify_ the lines in it somehow. It should just remove whole lines. You might want to open a question if that's the case, I'll keep an eye open.
paxdiablo
@paxdiablo I just tried to reproduce with a manually created test case and couldn't get it to reproduce. I know this happened, but maybe this command against my data wasn't the cause. I'll consider myself wrong until I can prove otherwise.
StarShip3000
A: 

How great is this! Great simple solution. Love it.

Chris