1

I want to remove an entire line of text from all files in a given directory. I know I can use grep -v foo filename to do this one file at a time. And I know I can use grep -r foo to search recursively through a directory. How do I combine these commands to remove a given line of text from all files in a directory?

PGilbert
  • 27
  • 7

3 Answers3

1

Try something like:

grep -vlre 'foo' . | xargs sed -i 's/pattern/replacement/g'

Broken down:

grep:
  -v 'Inverse match'
  -l 'Show filename'
  -r 'Search recursively'
  -e 'Extended pattern search'

xargs: For each entry perform

sed -i: replace inline 
jlhonora
  • 10,179
  • 10
  • 46
  • 70
1

The UNIX command to find files is named find, not grep. Forget you ever heard of grep -r as it's just a bad idea, here's the right way to find files and perform some action on them:

find . -type f -print | xargs sed -i '/badline/d'
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • But what if you want to edit **only** the files that **don't contain** a certain keyword? That's what I understood from the question, and that's why I suggested `grep -rv` – jlhonora Aug 11 '15 at 00:01
  • This almost works, but I get the following error `sed: 1: "./hr1_14_s.nex": invalid command code .` where hr1_14_s.nex is the name of the file I want to search inside of. Here is a link to a test file so that maybe the error can be recreated. https://github.com/PrincessG/SignalNoise/blob/master/Testfile. I'd like to remove the entire lines containing 'alligator_mississippiensis'. – PGilbert Aug 11 '15 at 01:16
  • change `-i` to `-i ""` as you're apparently using a sed that requires `-i` to have an arg after it (OSX?). – Ed Morton Aug 11 '15 at 16:25
  • @jlhonora the script I posted will only make changes to files that contain the keyword. – Ed Morton Aug 11 '15 at 16:27
0

I think this would work:

grep -ilre 'Foo' . | xargs sed -i 'extension' 'Foo/d'

Where 'extension' refers to the addition to the file name. It will make a copy of the original file with the extension you designated and the modified file will have the original filename. I added -i in case you require it to be case insensitive.

modified file1 becomes "file1" original file1 becomes "file1extension"

invalid command code ., despite escaping periods, using sed

One of the responses suggests that the newer version of sed's -i option in OSX is slightly different so you need to add an extension. The file is being interpreted as a command, which is why you are seeing that error.

Community
  • 1
  • 1