如何在避免“太多论点"的同时进行 grep [英] How can I grep while avoiding 'Too many arguments'

查看:29
本文介绍了如何在避免“太多论点"的同时进行 grep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试清除一些垃圾邮件时遇到了问题.队列中的文件数量太大,以至于我通常的命令无法处理.它会给我一个关于太多参数的错误.

I was trying to clean out some spam email and ran into an issue. The amount of files in queue, were so large that my usual command was unable to process. It would give me an error about too many arguments.

我通常这样做

grep -i user@domain.com 1US* | awk -F: '{print $1}' | xargs rm

1US* 可以是 1US[a-zA-Z] 之间的任何值.我唯一能做的就是运行这个可怕的装置.它的一个文件,包含 1USa、1USA、1USb 等,贯穿整个字母表.我知道他们必须是一种更有效地运行它的方法.

1US* can be anything between 1US[a-zA-Z]. The only thing I could make work was running this horrible contraption. Its one file, with 1USa, 1USA, 1USb etc, through the entire alphabet. I know their has to be a way to run this more efficiently.

grep -s $SPAMMER /var/mailcleaner/spool/exim_stage1/input/1USa* | awk -F: '{print $1}' | xargs rm
grep -s $SPAMMER /var/mailcleaner/spool/exim_stage1/input/1USA* | awk -F: '{print $1}' | xargs rm

推荐答案

运行多个 grep 实例.而不是

Run several instances of grep. Instead of

grep -i user@domain.com 1US* | awk '{...}' | xargs rm

(for i in 1US*; do grep -li user@domain "$i"; done) | xargs rm

注意 -l 标志,因为我们只想要匹配的文件名.这将加速 grep(在第一场比赛时终止)并使您的 awk 脚本不再需要.这可以通过检查 grep 的返回状态并调用 rm 来改进,而不是使用 xargs(xargs 非常脆弱,IMO).如果你问,我会给你更好的版本.

Note the -l flag, since we only want the file name of the match. This will both speed up grep (terminate on first match) and makes your awk script unrequired. This could be improved by checking the return status of grep and calling rm, not using xargs (xargs is very fragile, IMO). I'll give you the better version if you ask.

希望有帮助.

这篇关于如何在避免“太多论点"的同时进行 grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆