Unix Shell - Re: Most efficient way to delete files containing strings in long

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > February 2005 > Re: Most efficient way to delete files containing strings in long





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: Most efficient way to delete files containing strings in long
Bill Seivert

2005-02-24, 2:58 am



Jonathan Rogers wrote:
> Interesting problem that I hope isn't N-P complete.
>
> If I've got an arbitrarily long list of N unique strings:
> abcdefghij
> lmnopqrstuv
> wxyz#@!%*
> 123456789000
> ....etc
>
> what's the most efficient means of deleting all files in a particular
> (large) directory that contains any one of the strings in the list? I
> know I could do a bash script that ran through the string list with
> xargs, then grepped every file in the directory for each string in
> turn, then deleted the files that match, but is there a simpler or more
> efficient way? (A simplification is that no string is a substring of
> another; also, each file will contain only one of the strings, and will
> contain it within the first twenty lines or so if it exists in the file
> at all.)
>
> Respond via email and I'll summarize/post the best solution(s) back if
> anyone's interested.
>
> thanks /jon/
>
> (PS: okay, the string list is actually a list of unique Message-IDs,
> and the files are individual email messages, and in essence I want to
> delete all the emails that match my list of Message-Ids. It would be
> easy if the file names were the message-ids, but of course they're not.
> I mention this in case the actual application sparks someone's
> imagination...
>


Another possible untested solution:
Assuming str1 is one string, str2 is another string, etc.,
(Strings with embedded "|" would need them escaped),

egrep -l "$str1|$str2|$str3" file ... | xargs rm -f

I would think that when -l is specified, egrep would be smart
enough to stop searching the current file once it finds any match
and go to the next file.

Bill Seivert

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com