Generate record of files which have been removed b

2019-09-04 03:49发布

问题:

I asked a question here to remove unwanted lines which contained strings which matched a particular pattern:

Remove lines containg string followed by x number of numbers

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

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?

回答1:

Drop grep and use awk instead:

awk '/\<vol([[:blank:]]+[[:digit:]]+){2}/{print >> "deleted"; next} 1' file

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


标签: linux bash grep