Replace all occurrences without changing file endings

╄→尐↘猪︶ㄣ 提交于 2020-01-06 01:33:29

问题


To replace all occurrences of a word with another in all files with a particular extension in the current directory I use this command:

find . -type f -name '*.zz' -exec sed -i '' "s/replace_this/with_this/" {} +

The problem is that it adds \n to the end of files that do not end in \n. Now don't get me wrong, I love \n terminated files as much as the next person. But I don't want all those changes in my git diff. What's a command to run that does the same thing but without touching file endings?

By the way I'm using emacs with counsel-projectile so if there's a handy tool there instead that would be fine.

macOS 10.13.4


回答1:


Give a try to perl pie instead of sed with something like this:

perl -pi -e 's/replace_this/with_this/"

In your case:

find . -type f -name '*.zz' -exec perl -pi -e  "s/replace_this/with_this/" {} +


来源:https://stackoverflow.com/questions/50122548/replace-all-occurrences-without-changing-file-endings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!