Cra*_*ash 21 linux bash awk echo cat
如何在没有换行符的情况下将字符串添加到文件末尾?
例如,如果我使用>>它将添加到文件的末尾与换行符:
cat list.txt
yourText1
root@host-37:/# echo yourText2 >> list.txt
root@host-37:/# cat list.txt
yourText1
yourText2
Run Code Online (Sandbox Code Playgroud)
我想在yourText1之后添加yourText2
root@host-37:/# cat list.txt
yourText1yourText2
Run Code Online (Sandbox Code Playgroud)
Fra*_*anz 61
您可以使用echo的-n参数.像这样:
$ touch a.txt
$ echo -n "A" >> a.txt
$ echo -n "B" >> a.txt
$ echo -n "C" >> a.txt
$ cat a.txt
ABC
Run Code Online (Sandbox Code Playgroud)
编辑:啊哈,你已经有一个包含字符串和换行符的文件.好吧,无论如何我会把它留在这里,我们可能对某人有用.
fed*_*qui 11
只需使用printf
,因为它不会默认打印新行:
printf "final line" >> file
Run Code Online (Sandbox Code Playgroud)
让我们创建一个文件,然后添加一个没有尾随新行的额外行.注意我用cat -vet
来看新线.
$ seq 2 > file
$ cat -vet file
1$
2$
$ printf "the end" >> file
$ cat -vet file
1$
2$
the end
Run Code Online (Sandbox Code Playgroud)
sed '$s/$/yourText2/' list.txt > _list.txt_ && mv -- _list.txt_ list.txt
Run Code Online (Sandbox Code Playgroud)
如果您的sed实现支持-i选项,您可以使用:
sed -i.bck '$s/$/yourText2/' list.txt
Run Code Online (Sandbox Code Playgroud)
使用第二种解决方案,您也将拥有备份(首先您需要手动执行).
或者:
ex -sc 's/$/yourText2/|w|q' list.txt
Run Code Online (Sandbox Code Playgroud)
要么
perl -i.bck -pe's/$/yourText2/ if eof' list.txt
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
48547 次 |
最近记录: |