Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 为什么sed要向文件中添加新行,以及如何防止它?_Macos_Command Line_Sed - Fatal编程技术网

Macos 为什么sed要向文件中添加新行,以及如何防止它?

Macos 为什么sed要向文件中添加新行,以及如何防止它?,macos,command-line,sed,Macos,Command Line,Sed,如何使用以下命令防止sed在文件末尾添加新行: find . -not -path "./.git/*" -type f -name '*' -exec sed -i '' 's/password1/passworddeleted/g; s/password2/passworddeleted/g;' {} + 编辑: 为了证明我的问题: mkdir test; cd test; printf "no new lines" > no-new-line.txt; cat -e no-new-

如何使用以下命令防止sed在文件末尾添加新行:

find . -not -path "./.git/*" -type f -name '*' -exec sed -i '' 's/password1/passworddeleted/g; s/password2/passworddeleted/g;' {} +
编辑: 为了证明我的问题:

mkdir test; cd test; printf "no new lines" > no-new-line.txt; cat -e no-new-line.txt; find . -not -path "./.git/*" -type f -name '*' -exec sed -i '' 's/password1/passworddeleted/g; s/password2/passworddeleted/g;' {} +; cat -e no-new-line.txt;
将输出
无新行无新行$
cat-e
在mac上显示非打印字符。

sed
总是以换行符结束其输出,您不能强制它执行其他操作

发件人:

每当模式空间写入标准输出或命名文件时,sed应立即在其后换行


另一种选择是,您可以使用
sed
以外的内容,也可以去除
sed
输出的最后一个字符。

您可以使用此功能,或者与管道结合使用:

truncate -s -1 <file>
truncate-s-1

(这将删除文件的最后一个字节)

这不会向我添加任何新行。你在什么操作系统上运行这个?试着也提供一个例子。我添加了一个例子。谢谢你把它指给我看。很有趣。在CentOS,我没有收到任何新的线路。有趣的是,我问了一些类似的问题,关于程序是否添加新行,而
sed
显然没有。在中查看它。没有终止换行符的文件不是文本文件,因此您不应该期望像sed、awk、grep或。。。能够以任何特定的方式处理它。请参阅。@EdMorton文本文件可能有0行,因此不能用换行符定义。