Mailing List Archive


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

[tlug] sed / shell / redirection question



Mark Makdad writes:

 > Which seemed to work?  I'm not sure, I tried many things, and when I 
 > executed sed by itself on the shell I got the desired results.

Are you sure you executed "sed ... $X > $X"?

 > Why did this take all of my input files and truncate them at zero bytes?
 > 
 > Am I screwing up a basic redirection thing there?

Yes.  Redirections are done by the shell, and happen *before* the
program (here, sed) gets control.  You can't win this one.  You could
set NOCLOBBER=OhMuhGawdNo and run it, in which case you get a "can't
open, file exists" error.  You could use ">>" instead of ">", but that
would probably give you an infinite loop (depends on how big the files
are and how big stdin's buffer is, I think).  (If you were very lucky,
you'd get the desired output appended to the original file.)  Or you
do what you did, in which case the shell truncates the file before
redirecting output to it.

I think that "truncate" is the right word here, but if you're in the
mood to experiment, you could try "sed ... < $X > $X".  If rather than
truncate it unlinks the file, then you'll get input from the open (but
now anonymous) original file, and output will go to the newly linked
empty file that gets created.

I always use a temporary file ("sed ... $X > tmp$$$ && mv tmp$$$ $X")
rather than trying to edit in place.

An alternative would be to use Big Brother Perl (ie, "perl -ani") or
some variant (the crucial thing is the -i "inplace" option).  I'm not
a big fan of Perl, but that is definitely tr

  And also, does anyone 
 > have a stiff drink?   I just overwrote 20 files and just lost about an 
 > hour of work (yay backups).  My mistake, though, for playing with fire 
 > without backing up.
 > 
 > Cheers,
 > 
 > Mark
 > 
 > -- 
 > To unsubscribe from this mailing list, 
 > please see the instructions at http://www.tlug.jp/list.html
 > 
 > Please visit our sponsor at http://www.primustel.co.jp/tlug/


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links