Redirect output from sed 's/c/d/' myFile to myFile

后端 未结 10 830
暖寄归人
暖寄归人 2020-12-09 10:15

I am using sed in a script to do a replace and I want to have the replaced file overwrite the file. Normally I think that you would use this:

% sed -i \'s/c         


        
相关标签:
10条回答
  • 2020-12-09 10:58

    -i option is not available in standard sed.

    Your alternatives are your third way or perl.

    0 讨论(0)
  • 2020-12-09 11:03

    If you don't want to move copies around, you could use ed:

    ed file.txt <<EOF
    %s/cat/dog/
    wq
    EOF
    
    0 讨论(0)
  • 2020-12-09 11:03

    To change multiple files (and saving a backup of each as *.bak):

    perl -p -i -e "s/oldtext/newtext/g" *

    replaces any occurence of oldtext by newtext in all files in the current folder. However you will have to escape all perl special characters within oldtext and newtext using the backslash 
    
    This is called a “Perl pie” (mnemonic: easy as a pie)
    The -i flag tells it do do in-place replacement, and it should be ok to use single (“'”) as well as double (“””) quotes.
    
        If using ./* instead of just *, you should be able to do it in all sub-directories 
    See man perlrun for more details, including how to take a backup file of the original.
    using sed:
                sed -i    's/old/new/g' ./*  (used in GNU)
        sed -i '' 's/old/new/g' ./*  (used in FreeBSD)
    
    0 讨论(0)
  • 2020-12-09 11:04

    You can use sponge from the moreutils.

    sed "s/cat/dog/" manipulate | sponge manipulate
    
    0 讨论(0)
提交回复
热议问题