Find and replace in file and overwrite file doesn't work, it empties the file

后端 未结 13 2258
深忆病人
深忆病人 2020-11-22 08:45

I would like to run a find and replace on an HTML file through the command line.

My command looks something like this:

sed -e s/STRING_TO_REPLACE/STR         


        
13条回答
  •  情深已故
    2020-11-22 09:27

    With all due respect to the above correct answers, it's always a good idea to "dry run" scripts like that, so that you don't corrupt your file and have to start again from scratch.

    Just get your script to spill the output to the command line instead of writing it to the file, for example, like that:

    sed -e s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html
    

    OR

    less index.html | sed -e s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g 
    

    This way you can see and check the output of the command without getting your file truncated.

提交回复
热议问题