Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a giant file with thousands of lines, I want to delete only the lines which have the word "Foo" in them.

How would I go about doing this? I'm on a Mac, so I could use some command line utility, but I'd prefer to use TextWrangler.

share|improve this question

7 Answers 7

up vote 1 down vote accepted

Most graphical programmer's editors do not allow you to delete lines like this, but only change their contents. This can be done by replacing ^.*Foo.*$ with nothing.

If you're willing to look towards an external tool then this can be done by filtering through sed "/Foo/d".

share|improve this answer
    
I wound up using sed, but that ^.Foo.*$ would work with TextWrangler. thanks! –  Zee Jul 12 '10 at 19:39
    
So would sed, as a filter. –  Ignacio Vazquez-Abrams Jul 12 '10 at 19:54

In TextWrangler: Text > Process Lines Containing…

share|improve this answer

Use a regular expression to find the lines, then replace them with an empty string.

Here's a page that explains how to construct the regular expression:

http://www.regular-expressions.info/completelines.html

To do this in TextWrangler:

  1. Choose Find from the Search menu.
  2. Check the Grep checkbox.
  3. Enter the regular expression in the Find: text box.
  4. Select Next.
share|improve this answer

A quick command line for this is:

grep -v myword myfile > newfile
share|improve this answer

If you search-and-replace ^.*Foo.*\r with nothing (with "Use Grep" enabled), it'll completely get rid of lines with "Foo" in them, not just blank them out. The only limit is that it won't work on the last line in the file. (Replacing \r.*Foo.*$ with nothing would work on the last line, but not the first.)

share|improve this answer

This perl command line will do the trick!

perl -ni -e "print unless /Foo/" large_file.txt

you could also do multiple files:

perl -ni -e "print unless /Foo/" *.txt
share|improve this answer

Bare Bones TextWrangler http://www.barebones.com/products/textwrangler/

Text Menu Process Lines Containing… Menu Item

Enter text to delete Then choose delete lines

Confirm

Process Lines Containing

share|improve this answer

Your Answer

 
discard

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.