TIMEX
TIMEX

Reputation: 272452

How do I remove lines from every file that has a matching text?

$ grep "console.log" * -R
account/db.js:        console.log(err);
account/db.js:        console.log(info);
account/db.js:                console.log(err);
account/db.js:                    console.log(err2);
account/controller.js:    console.log(covers);
account/controller.js:    console.log(req.api_user);
account/controller.js:    console.log(code);
account/controller.js:        console.log(user);
account/helper.js:                console.log(err);
messages/db.js:        console.log("Error " + err);
messages/helper.js:                console.log('No email notify.');
messages/helper.js:                console.log(msg_body);
messages/helper.js:                        console.log(message.sid);
messages/helper.js:                        console.log(message.dateCreated);
messages/helper.js:                        console.log(error);
products/controller.js:            console.log(product);
products/controller.js:                console.log(product);
products/helper.js:        console.log(data)
products/helper.js:    console.log('removing index....');
profile/db.js:                console.log(err);
profile/db.js:                console.log(info);
profile/db.js:                console.log(err);
profile/controller.js:                            console.log("sending phone confirmation text...");
profile/helper.js:            console.log(message.sid);
profile/helper.js:            console.log(message.dateCreated);
profile/helper.js:            console.log(error);
receiver/controller.js:    console.log(from);
receiver/controller.js:    console.log(body);
receiver/controller.js:        console.log(from_email);
receiver/controller.js:        console.log(to_id_gen);
receiver/controller.js:        console.log(finalbody);
receiver/controller.js:                            console.log(result);
reviews/db.js:        console.log(err);
reviews/db.js:        console.log(results);
reviews/controller.js:            console.log(review);
reviews/controller.js:    console.log(review_id);
search/controller.js:        console.log(JSON.stringify(data.hits.hits, null, 4));

As you can see, while I was writing my code, I was doing console.log everywhere.

Now, I want to remove all those lines. I don't want to manually go into every file to remove them. Instead, I want to do it via a command.

Similar to grep "console.log" * -R, how can I do that same thing but remove those lines recursively? (look through every file all the way down the tree from my current directory)

Upvotes: 0

Views: 758

Answers (3)

Zombo
Zombo

Reputation: 1

Give this a shot.

sed -i.bak '/console.log/d' */*.js

The -i param edits files in place. The original file will have .bak appended to its name, so you can restore it if something goes wrong, or you change your mind.

This is not a properly recursive solution; you may need to modify the file name wildcard, or drive the command from a find . -exec ... wrapper to make it recursive.

Upvotes: 0

abasu
abasu

Reputation: 2524

try find for such things. try find . -type f -exec sed -i '/console\.log/d' {} \; but you might be interested in using -i.bak instead of -i which keeps a back up copy

Upvotes: 1

Andy Lester
Andy Lester

Reputation: 93815

In Perl:

perl -i -n -e'print unless /console\.log/' $(find . -name '*.js')

Upvotes: 0

Related Questions