Say what? was Re: [lug] Changing files on-the-fly

The Matt thompsma at colorado.edu
Tue Sep 16 10:16:03 MDT 2003


On Tue, 2003-09-16 at 08:31, Jeff Schroeder wrote:
> I've been writing shell scripts for years, and as I write more complex 
> ones I'm repeatedly annoyed by the fact that if I want to change a text 
> file on-the-fly I have to redirect it to a temp file.  For example, 
> let's say I have a four-line text file called numbers.txt:
> 
> one
> two
> three
> four
> 
> I want to remove the line containing 'three' from the file, so I'm left 
> with
> 
> one
> two
> four
> 
> In order to do this (AFAIK) I have to do this:
> 
> grep -v three numbers.txt > .tempfile && mv .tempfile numbers.txt
> 
> Because if I try to do it all at once, via
> 
> grep -v three numbers.txt > numbers.txt
> 
> The resulting file is empty.  Is there a way to NOT use a temporary file 
> for this sort of operation?  I'm not just talking about grep here; I'm 
> including sed or any other tool that would alter a file's contents.  
> Maybe this is just The Unix Way, but I figured I'd ask anyway. :)

Uhhhh...help me please.  I decided to try out Mr. Schroeder's thing
here, you know, for fun.

So:
-- ixion:~/Test --
> cat trial.txt
one
two
three
four
-- ixion:~/Test --
> grep -v three trial.txt > numb.txt
-- ixion:~/Test --
> cat numb.txt
one
two
four
-- ixion:~/Test --
>

Huh.  Now, maybe this is the wacky world of zsh (the emacs of the shell
world), or a different version of grep...but why *does* this work?

Matt

-- 
"And isn't sanity really just a one-trick pony, anyway?  I mean,
all you get is one trick, rational thinking, but when you're good
and crazy, ooh ooh ooh, the sky's the limit!" -- The Tick
  The Matt -- http://ucsub.colorado.edu/~thompsma/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20030916/d8965495/attachment.pgp>


More information about the LUG mailing list