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.
Best Answer
With a GNU sed:
With an OSX sed:
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 offind
, grabs a list based on a separator (\0
because of-0
), invokessed -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 expressionKeyWord
.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: