繁体   English   中英

如何使用 append 在文件末尾的一行使用 sed

[英]How to append a line at the end of the file using sed

假设 test.txt 文件已经存在,以下命令可以正常工作:

sed -i -e '$a\
This line was appended at the end.' test.txt

但是这个命令需要两行。 我宁愿有一条线,就像这样:

sed -i -e '$a\ This line was appended at the end.' test.txt

但不幸的是,它引发了一个错误:

sed: 1: "$a\ This line was appended at the end.": extra characters after \ at the end of a command

是否有可能让这个命令作为单行工作?

您可以使用$'\n' shell 转换为换行符:

sed -i -e '$a\'$'\n''This line was appended at the end.' test.txt

或者

sed -i -e $'$a\\\nThis line was appended at the end.'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM