none
The reverse of the findstr command in a batch file RRS feed

  • Question

  • Hi.

    I hope I'm in the right place.  I am trying to run a command/batch file on a really large text file to replace all lines of text with the exception of certain lines which contain a certain word.  I know this can be done to 'delete' certain lines and keep the others using the "findstr" command.  What this command does is deletes all lines in a text file that contain specific words whilst keeping all other lines.

    I want to do the exact opposite to this.  I want to keep certain lines of text containing specific words whilst deleting all others.

    Does anyone know what this command might be?  Or is there a switch one can use with "findstr" command to do it?

    All help appreciate

    J

    Wednesday, April 30, 2014 1:52 PM

Answers

All replies

  • If you use findstr to locate strings within a text file, you can echo only those lines and redirect them to a new file.


    -- Bill Stewart [Bill_Stewart]


    Wednesday, April 30, 2014 2:20 PM
    Moderator
  • In general you could write:

    (Get-Content.\inputfile.txt) -match"keyword"|Out-File.\outputfile.txt-Encodingascii

    Outputfile.txt would then contain all lines that contain "keyword".

    This should work for files with a size <4GB.
    I've just tried: The powershell.exe process didnt get more than 4GB RAM. :-)

    Walter

    Wednesday, April 30, 2014 3:05 PM