Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a large text file with a size of more than 30 megabytes. I want to remove all the lines which don't match some specific criteria, e.g. lines that don't have the string 'START'.

What's the easiest way to do this?

share|improve this question
What operating system are you using? – MrStatic Jun 3 '10 at 1:52
@MrStatic: MacOS – Shawn Jun 3 '10 at 1:54

If the pattern is really that simple, grep -v will work:

grep -v START bigfile.txt > newfile.txt

newfile.txt will have everything from bigfile.txt except lines with "START".

(In case it isn't obvious, this is something you'll do in Terminal or other command line tool)

share|improve this answer

The original question asked how to remove the lines that didn't match a pattern. In other words, how to keep the lines that do match the pattern. Thus, no need for -v.

grep START infile.txt > outfile.txt

Note that grep can use regular expressions to do much more powerful pattern matching. The syntax is a bit obtuse though.

share|improve this answer

Use GNU sed with the -i argument.

share|improve this answer
to make the answer a bit more .. verbose: "sed -n -e '/START/p' inputfile". and maybe it is a good idea not to use -i while playing around with file altering commands, just in case. – akira Jun 3 '10 at 2:28
grep -v START inputfile

should work. grep is standard on both MacOS and Linux/Unix, can be installed on MS Windows.

Option -v is for in*v*erting the match - only output lines that do not contain the pattern (the inverse of the usual grep behaviour).

share|improve this answer

For Windows Command Prompt (help find for options):

find /v "START" original_file.txt > new_file.txt

For Linux, OS X, etc. (man grep for options):

grep -v "START" original_file.txt > new_file.txt

For more complicated text matching grep offers a lot more functionality than find. If you are on Windows you can easily find a port of grep or you can use Windows' findstr instead of find.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.