sed edit file in place

前端 未结 13 1936
忘掉有多难
忘掉有多难 2020-11-22 05:20

I am trying to find out if it is possible to edit a file in a single sed command without manually streaming the edited content into a new file and

13条回答
  •  醉梦人生
    2020-11-22 05:56

    sed supports in-place editing. From man sed:

    -i[SUFFIX], --in-place[=SUFFIX]
    
        edit files in place (makes backup if extension supplied)
    

    Example:

    Let's say you have a file hello.txtwith the text:

    hello world!
    

    If you want to keep a backup of the old file, use:

    sed -i.bak 's/hello/bonjour' hello.txt
    

    You will end up with two files: hello.txt with the content:

    bonjour world!
    

    and hello.txt.bak with the old content.

    If you don't want to keep a copy, just don't pass the extension parameter.

提交回复
热议问题