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 managed to grep the occurences of a keyword by using

grep "KeyWord" . -r -n -i -I 

but, the occurences of the KeyWord are too large. I just want to delete all the lines containing that word.

I searched around, sed looks like to be the utility. Can anyone tell me what command would do the trick?

Thanks.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

With a GNU sed:

find . -type f -print0 | xargs -0 sed -i /KeyWord/d

With an OSX sed:

find . -type f -print0 | xargs -0 sed -i '' /KeyWord/d

First command find finds all the standard files (not directories, or pipes, or etc.), prints them separated by \0 (so filenames can contains spaces, newlines, etc.).

Second command xargs reads the output of find, grabs a list based on a separator (\0 because of -0), invokes sed -i [...] with added parameters from the list (sed will be called multiple times if there are a lot of files, as the maximum length of the parameters is limited in each invocation).

The sed command modifies in-place (-i).

As to /KeyWord/d, it'll delete lines containing the regular expression KeyWord.

You should learn sed to properly understand the (simple but unusual) syntax, and refer to the appropriate manpages for more information about the tools involved here.


And as I like to promote zsh, the solution with its extended globs:

sed -i /KeyWord/d **/*(.)
share|improve this answer
    
How about just deleting the word "keyword." and not the whole line? –  snihalani Jul 11 '12 at 23:39
    
s/KeyWord//g (substitutes KeyWord with '' (empty string), g for all (not only the first on each line). –  Pierre Carrier Jul 15 '12 at 17:23
1  
Why don't you give it a try yourself, instead of asking me? –  Pierre Carrier Jul 25 '12 at 9:57

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.