生成由grep删除的文件的记录,作为主要命令的辅助功能 [英] Generate record of files which have been removed by grep as a secondary function of primary command
问题描述
我在这里问了一个问题,以删除不需要的行,这些行包含与特定模式匹配的字符串:
I asked a question here to remove unwanted lines which contained strings which matched a particular pattern:
anubhava提供了很好的代码行,完全可以满足我的需求.此代码删除包含字符串vol
,后跟一个空格以及三个或更多连续数字的任何行:
anubhava provided a good line of code which met my needs perfectly. This code removes any line which contains the string vol
followed by a space and three or more consecutive numbers:
grep -Ev '\bvol([[:blank:]]+[[:digit:]]+){2}' file > newfile
该命令将在相当大的csv文件中使用,并由crontab启动.出于这个原因,我想保留此命令要删除的行的记录,只是为了回过头来检查要删除的正确数据,我想这将是某种包含该行名称的日志并没有进入最终裁定.如何添加此功能?
The command will be used on a fairly large csv file and be initiated by crontab. For this reason, I would like to keep a record of the lines this command is removing, just so I can go back to check the correct data that is being removed- I guess it will be some sort of log containing the name sof the lines that did not make the final cut. How can I add this functionality?
推荐答案
拖放grep并改用awk:
Drop grep and use awk instead:
awk '/\<vol([[:blank:]]+[[:digit:]]+){2}/{print >> "deleted"; next} 1' file
上面的代码使用GNU awk作为字定界符(\<
),并将每条删除的行附加到名为"deleted"的文件中.也考虑添加时间戳:
The above uses GNU awk for word delimiters (\<
) and will append every deleted line to a file named "deleted". Consider adding a timestamp too:
awk '/\<vol([[:blank:]]+[[:digit:]]+){2}/{print systime(), $0 >> "deleted"; next} 1' file
这篇关于生成由grep删除的文件的记录,作为主要命令的辅助功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!