替换所有出现的内容而不更改文件结尾 [英] Replace all occurrences without changing file endings

查看:74
本文介绍了替换所有出现的内容而不更改文件结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在当前目录中具有特定扩展名的所有文件中用一个单词替换所有出现的单词,请使用以下命令:

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/" {} +

问题在于,它会将 \ n 添加到未以 \ n 结尾的文件的末尾.现在不要误会我的意思,我爱 \ n 终止的文件与下一个人一样多.但是我不想在git diff中进行所有这些更改.可以执行什么操作但又没有碰到文件结尾的命令?

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?

顺便说一句,我将Emacs与Advisor-Projectile结合使用,因此,如果有一个方便的工具,那会很好.

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

macOS 10.13.4

推荐答案

尝试使用以下类似方法尝试 perl pie ,而不是 sed 这个:

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

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

在您的情况下:

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

这篇关于替换所有出现的内容而不更改文件结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆