Using sed and grep/egrep to search and replace

前端 未结 6 831
小鲜肉
小鲜肉 2020-11-30 16:47

I am using egrep -R followed by a regular expression containing about 10 unions, so like: .jpg | .png | .gif etc. This works well, now I would like

6条回答
  •  无人及你
    2020-11-30 17:26

    I couldn't get any of the commands on this page to work for me: the sed solution added a newline to the end of all the files it processed, and the perl solution was unable to accept enough arguments from find. I found this solution which works perfectly:

    find . -type f -name '*.[hm]' -print0 
        | xargs -0 perl -pi -e 's/search_regex/replacement_string/g'
    

    This will recurse down the current directory tree and replace search_regex with replacement_string in any files ending in .h or .m.

    I have also used rpl for this purpose in the past.

提交回复
热议问题