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

Re: sed -i turns link into file



Vincent Lefevre wrote:
On 2008-01-18 02:54:12 +0100, Александър Л. Димитров wrote:
I don't think it's a bug, but a feature.

It's at least a documentation bug. The sed(1) man page says:

       -i[SUFFIX], --in-place[=SUFFIX]

              edit files in place (makes backup if extension supplied)

I'd say that if it's "in place", the symbolic link should be preserved
just like with "echo foo > file". But this is rather ambiguous.

Note that perl has the same behavior as sed, but its man page is
explicit about what is done:

   -i[extension]
       specifies that files processed by the "<>" construct are to be
       edited in-place.  It does this by renaming the input file, opening
       the output file by the original name, and selecting that output
       file as the default for print() statements.  [...]


This is a documentation issue, for sure. UNIX interactive editors I'm familiar with will create a temporary file for editing use, leaving the original intact until an explicit 'save' is done. This will "preserve" a symlink, if that's what the original was.

The stream processing tools, such as sed and awk, leave the original intact, applying changes to the stream of data read, and sending the result to standard output. Editing "in place" preserves this general way of processing.

To edit "in place" is done by renaming the original to be the 'temp' file and writing the results to a "new" file with the same name as the original. This is more efficient than the interactive way of doing things, since only one read/write cycle is done, rather than two.

The solution is to write a script that does things the first way. The script would copy the original file to a temp name, then run sed on the temp name with I/O redirected to the original file.

--
Bob McGowan

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


Reply to: