Menu

#477 SED 4.2 --in-place - Broken (Again)

Sed
closed
GnuWin
Binaries (396)
7
2013-02-11
2009-06-04
Jak Frost
No

The --in-place switch is broken (again).

It works fine in SED 4.1.4, 4.1.5 but 4.2 is broken. I reported this same problem with a previous ported GnuWin32 version of SED a few years back. Search for it.


Error:

sed.exe: cannot rename ./sedv4PHwv: File exists


Example:

sed.exe --in-place --regexp-extended --unbuffered --expression="/^[[:space:]][/ h; /^[[:space:]]%~3[[:space:]]=/I { x; /^[[:space:]][[[:space:]]%~2[[:space:]]]/I { x; s/^([[:space:]]%~3[[:space:]]=[[:space:]])(.)[[:space:]]*$/\1%~4/I; x; }; x; };" "%filePath%"

Discussion

  • Jak Frost

    Jak Frost - 2009-06-07

    The new SED 4.2-1 update removes the --in-place option completely! Please fix the problem like you did in a previous SED release, but do not remove this option.

    sed.exe: unrecognized option `--in-place'

     
  • GnuWin

    GnuWin - 2009-06-07

    For example:

    sed --in-place -e s/a/b/ aaa

    where aaa contains some lines, works for me.
    Could you create an example that doesn't work and post an exact copy of your command line and its results?

     
  • Jak Frost

    Jak Frost - 2009-06-07

    Disregard the previous comment about the missing "--in-place" option, had a old SED 3.02 release in a folder in my PATH.

    The 4.2-1 release works with the --in-place switch but it leaves the temporary file behind without deleting it inside the Working Folder for the batch file, and not where the source file is located.

    Command: sed.exe --in-place --regexp-extended --unbuffered --expression="%~2" "%filePath%"

     
  • Jak Frost

    Jak Frost - 2009-06-08

    Here is an example script to use to reproduce this bug.


    @ echo off

    echo + Create "test.txt" file...
    echo search> "test.txt"

    echo * Replace "search" with "replace" in "test.txt"...
    sed.exe --in-place --regexp-extended --unbuffered --expression="s/search/replace/" "test.txt"

    echo ? Check if "sed." temporary file still exists...
    if exist "sed
    ." echo - Failed. Bug: The temporary file still exists...
    if not exist "sed*." echo + Success. No temporary files found.

    echo ? Query sed version...
    sed.exe --version | sed.exe --regexp-extended --unbuffered --quiet --expression="1 p;"

     
  • Jak Frost

    Jak Frost - 2009-06-08

    For reference, I'm running Windows XP SP3.

     
  • Lorenzo Cappelletti

    I'm experiencing the same problem, too: when using option "-i" or "--in-place", sed leaves behind a temporary file of pattern "sed??????".

    This is a slightly modified batch file derived from jakfrost's version which reproduce the problem. It creates a sed_test.txt file and runs sed over it using option "-i". Then, it checks whether a temporary file of pattern "sed??????" is left behind in the same directory where sed was run from.


    @echo off

    echo + Create "sed_test.txt" file...
    echo foo> sed_test.txt

    echo * Replace "foo" with "bar" in sed_test.txt...
    sed.exe -i -e "s/foo/bar/" sed_test.txt

    echo ? Check if "sed??????" temporary file still exists...
    if exist "sed??????" echo - Failed: the temporary file still exists!
    if not exist "sed??????" echo + Success. No temporary files found.

    echo ? Query sed version...
    sed.exe --version | sed.exe -ne "1p"

     
  • James Athey

    James Athey - 2010-09-01

    Like lolo75, I'm also finding that using -i or --in-place leaves the temporary files behind. I noticed that the GnuWin32 version of sed adds the following three lines to the options handling for "-i" in sed.c:

    ifdef _WIN32

      copy_instead_of_rename = true;
    

    endif / _WIN32 /

    Why was this necessary? The ck_rename() function has WIN32 specific code in it already; does it not work? Shouldn't sed clean up after itself?

     
  • GnuWin

    GnuWin - 2010-12-27

    This bug has been fixed. A new release is available through
    http://gnuwin32.sourceforge.net/
    Thanks for reporting the bug.

     
  • Anonymous

    Anonymous - 2013-02-11

    If somebody else is experiencing the bug, and in a hurry, I recommend the following workaround a batch script to be invoked through

    call sed.bat
    

    Create a temporary directory and set it as a working directory. Invoke SED, restore original working directory and delete all the temporary files.

    See an example implementation at https://gist.github.com/Pinault/4756362

    -- The scripts minimal content, in my case is:
    (You just need to edit the path to your SED executable)

    if not exist "%TEMP%\sed_wd" mkdir "%TEMP%\sed_wd"
    pushd "%TEMP%\sed_wd"
    @"%~f0\..\gnuwin32\%~n0.exe" %*
    del /q sed*
    popd
    rmdir "%TEMP%\sed_wd"
    

    Regards,
    P.

    -- WARNING: Please note that if the permissions on your current working directory are specific, going through a temporary file is likely to break the permissions set on that file. In that case it is better to rename the original file, process it and redirect the output of sed in the original working directory in place of the original file (optional: delete the original that was renamed).

    if not exist %web_config%.original copy %web_config% %web_config%.original
    > %web_config% sed.exe --unbuffered --expression="%repl_pattern%" %web_config%.original
    
     

    Last edit: Anonymous 2018-10-12