[lug] Changing files on-the-fly

rm at fabula.de rm at fabula.de
Tue Sep 16 09:21:36 MDT 2003


On Tue, Sep 16, 2003 at 08:31:39AM -0600, Jeff Schroeder wrote:
> [...]

> 
> 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. :)

Not with the given syntax. The shell does redirection by first opening the
files you redirect from/to and then spawning the program(s) with the 
file descriptors set to the allready opened files. So when, to take your
example, 'grep' starts and opens 'numbers.txt' that file is allready opened
for writing by the shell. And since the shell opens the file truncating
it will be empty. It's not really possible to overcome this unless all
involved parts coordinate. For such tasks i usually end up using perl 
which _can_ do in-place editing.

 hth Ralf Mattes
> 
> TIA,
> Jeff
> 
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug



More information about the LUG mailing list