On 2018-08-13 06:40, Tim Chase wrote:
Synopsis:       "sed -i" not renaming/moving temp-file

sed -i works just fine for me. It's really the use of ';q' that seems to cause the behavior.

This seems like expected behavior for sed when ;q is present.


Description:

"sed -i.bak '/2/d;q' file.txt" leaves temp file instead of renaming.

How-To-Repeat:

 $ mkdir sedbug
 $ cd sedbug
 $ jot 5 > file.txt
 $ sed -e '/2/d;q' file.txt # expected behavior: 1 line
 1
 $ sed -i.bak -e '/2/d;q' file.txt # edit it in-place
 $ ls
 file.txt sedpK7uniHbMN
 $ cat file.txt # still has original contents
 1
 2
 3
 4
 5
 $ cat sedpK7uniHbMN
 1

Expected results:

 the resulting temp-file (which contains the correct data) should be
moved back atop the original source file(s).



What does "q" do?  Its not commonly used.  From SED(1):
    [1addr]q
Branch to the end of the script and quit without starting a new
            cycle.


This is probably the command line you actually want, no '...;q' needed - will delete any line with "2" in it in file.txt, and will save the old one to file.txt.bak:

sed -i.bak -e '/2/d' file.txt


By putting ";q" in the sed command string, sed is told to cut out at the end of a loop. In OpenBSD, this will mean your particular sed command will exit out after it makes a transformation in a temp file. MacOS does something similar. Linux cuts the temp file out mid-way, leaving a partial result. Its not totally surprising to me that sed is leaving the temp file, its only half way done, so to speak.


Reply via email to