[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: delete file based on content



On Tue, 06 Jan 2004 08:10:44 +0100, Jan Minar wrote:

> On Mon, Jan 05, 2004 at 07:28:31PM -0500, Paul Morgan wrote:
>> On Mon, 05 Jan 2004 18:18:00 -0600, Michael Martinell wrote:
>> > #rm `grep -li "Processing completed correctly" *`
>> > 
>> > 
>> > This worked nicely at the command line, however when I put in into a script
>> > I received the error rm :too few arguments
>> > 
>> > Any other thoughts?
>> > 
>> 
>> You would get that, at the command line or in a script, if there were no
>> filenames returned by grep (i.e. none to delete).  You can check by simply
>> typing "rm" by itself and you'll get the same error message.  To suppress
>> the error message, you could do this:
>> 
>> rm `grep -li "Processing completed correctly" *` 2>/dev/null
> 
> This is ugly, Paul: It'd suppress not only this message, but other
> messages which would possibly be worth noticing.  Given sufficient
> number of executions, such a message will appear--and we would miss it.

Jan,

Notice I said "could", not should.  I was of course aware that I am
sinking all error messages, but it was meant as an example, as I also
pointed the poster at the relevant section of the bash manpages who would
then have seen what the redirection did.  It was not my intention to write
a full script with complete error handling so he didn't have to see the
"too few arguments" message.

But thank you for being picky, Jan, even if you didn't offer an
alternative (joking), because it made me think.

So, seeing as you mentioned it (and replacing the archaic backticks):

fgrep -q "Processing completed correctly" * && rm $(fgrep -li "Processing completed correctly" *)

Kind of slow, what with the two passes, though.  So, one could get cute:

tf=$(tempfile);rm $(fgrep -li "Processing completed correctly" *) $tf;unset tf

or just

(tf=$(tempfile);rm $(fgrep -li "Processing completed correctly" *) $tf)

Figuring out the command lists above is left as an exercise for the
original poster, the point being that there is more than one way to skin a
cat.

-- 
....................paul

Programming without a hex editor is like watchmaking without a hammer.




Reply to: