簡體   English   中英

為什么 sed 向文件添加新行以及如何防止它?

[英]Why is sed adding a new line to the file and how to prevent it?

如何使用以下命令防止 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;

將輸出no new linesno new lines$ cat -e在 mac 上顯示非打印字符。

sed總是以換行符結束它的輸出,你不能強迫它這樣做。

來自POSIX man sed

每當模式空間被寫入標准輸出或命名文件時,sed 應立即在其后面添加一個換行符。

作為替代方案,您可以使用sed以外的其他內容或sed輸出的最后一個字符。

您可以使用它,或與管道結合使用:

truncate -s -1 <file>

(這將刪除文件的最后一個字節)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM