93

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.

0

5 Answers 5

129

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 
Sign up to request clarification or add additional context in comments.

20 Comments

To be complete: you need to add "> output.txt" (without quotes) to have the output written in a file.
@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 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.
thanks, this helped me out but a word of caution, I had some issues with phrases including spaces and had to use /c: so the command would look like this: type file.txt | findstr /v /c:"ERROR" | findstr /v /c:"REFERENCE". Also if you need to strip lines off that you know are at the top fo the file, pipe it through more +<n> where <n> is the number of lines to strip off.
why is it deleting everything in my file test.java if I write type test.java | findstr /v "ECHO is on." > test.java but it do what is expected if I write it to a new file like type test.java | findstr /v "ECHO is on." > test2.java
|
63

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.

6 Comments

type !INSTALLED_APPS! | findstr /v "@ //" > temp_filter_out_junk.txt >nul 2>&1
I tried this type D:/e5/datastream/package.json | findstr /v eikon-framework | findstr /v df-core but it doesn't delete the lines
I tried findstr /V "eikon-framework df-core" D:\e5\datastream\package.json > D:\e5\datastream\package.json but it deletes all the contents if I try replace the data in the same file and If I use different out put file name then it works. How do I update the same file?
@kittu Use ">>" instead of ">". From the answer: Use ">> outfile.txt" instead if you want to append. You can't alter the same file in that single command.
Well since the question is about replacing in a file, how do you alter the current file? Everything I tried just leads to the file being empty
|
7

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

1 Comment

Don't you need the -i flag?
1

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.