[英]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. 如您所见,在编写代码时,我到处都在进行console.log。
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? 类似于grep "console.log" * -R
,我如何做同样的事情,但是递归地删除那些行? (look through every file all the way down the tree from my current directory) (从我的当前目录一直浏览树中的每个文件)
try find
for such things. 尝试find
这种东西。 try find . -type f -exec sed -i '/console\\.log/d' {} \\;
尝试find . -type f -exec sed -i '/console\\.log/d' {} \\;
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 但是您可能会对使用-i.bak
而不是-i
感兴趣, -i.bak
它会保留备份副本
Give this a shot. 试一下。
sed -i.bak '/console.log/d' */*.js
The -i
param edits files in place. -i
参数可在适当位置编辑文件。 The original file will have .bak
appended to its name, so you can restore it if something goes wrong, or you change your mind. 原始文件的名称后将附加.bak
,因此如果出现问题或改变主意,则可以将其恢复。
This is not a properly recursive solution; 这不是一个适当的递归解决方案。 you may need to modify the file name wildcard, or drive the command from a find . -exec ...
您可能需要修改文件名通配符,或从find . -exec ...
驱动命令find . -exec ...
find . -exec ...
wrapper to make it recursive. find . -exec ...
包装使其递归。
在Perl中:
perl -i -n -e'print unless /console\.log/' $(find . -name '*.js')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.