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

后端 未结 13 2098
深忆病人
深忆病人 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:28
    sed -i 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' index.html
    

    This does a global in-place substitution on the file index.html. Quoting the string prevents problems with whitespace in the query and replacement.

    0 讨论(0)
  • 2020-11-22 09:36

    I was searching for the option where I can define the line range and found the answer. For example I want to change host1 to host2 from line 36-57.

    sed '36,57 s/host1/host2/g' myfile.txt > myfile1.txt
    

    You can use gi option as well to ignore the character case.

    sed '30,40 s/version/story/gi' myfile.txt > myfile1.txt
    
    0 讨论(0)
  • 2020-11-22 09:43

    Warning: this is a dangerous method! It abuses the i/o buffers in linux and with specific options of buffering it manages to work on small files. It is an interesting curiosity. But don't use it for a real situation!

    Besides the -i option of sed you can use the tee utility.

    From man:

    tee - read from standard input and write to standard output and files

    So, the solution would be:

    sed s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html | tee | tee index.html
    

    -- here the tee is repeated to make sure that the pipeline is buffered. Then all commands in the pipeline are blocked until they get some input to work on. Each command in the pipeline starts when the upstream commands have written 1 buffer of bytes (the size is defined somewhere) to the input of the command. So the last command tee index.html, which opens the file for writing and therefore empties it, runs after the upstream pipeline has finished and the output is in the buffer within the pipeline.

    Most likely the following won't work:

    sed s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g index.html | tee index.html
    

    -- it will run both commands of the pipeline at the same time without any blocking. (Without blocking the pipeline should pass the bytes line by line instead of buffer by buffer. Same as when you run cat | sed s/bar/GGG/. Without blocking it's more interactive and usually pipelines of just 2 commands run without buffering and blocking. Longer pipelines are buffered.) The tee index.html will open the file for writing and it will be emptied. However, if you turn the buffering always on, the second version will work too.

    0 讨论(0)
  • 2020-11-22 09:43
    sed -i.bak "s#https.*\.com#$pub_url#g" MyHTMLFile.html
    

    If you have a link to be added, try this. Search for the URL as above (starting with https and ending with.com here) and replace it with a URL string. I have used a variable $pub_url here. s here means search and g means global replacement.

    It works !

    0 讨论(0)
  • 2020-11-22 09:44

    You should try using the option -i for in-place editing.

    0 讨论(0)
  • 2020-11-22 09:45

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

    perl -p -i -e "s/\|/x/g" *  
    

    will take all files in directory and replace | with x this is called a “Perl pie” (easy as a pie)

    0 讨论(0)
提交回复
热议问题