击:字符串添加到文件末尾,没有断行 [英] Bash: add string to the end of the file without line break

查看:125
本文介绍了击:字符串添加到文件末尾,没有断行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加字符串到文件末尾,没有断行?

例如,如果我使用>>它会增加与换行符文件的末尾:

 猫LIST.TXT
yourText1
根@主机37:/#回声yourText2>> LIST.TXT
根@主机37:/#猫LIST.TXT
yourText1
yourText2

我想补充yourText2权yourText1后

 根@主机-37:/#猫LIST.TXT
yourText1yourText2


解决方案

 的sed'$ S / $ / yourText2 /'LIST.TXT> _list.txt_&功放;&安培; MV  -  _list.txt_ LIST.TXT

如果您的 SED 的实施支持的 -i 的选项,你可以使用:

  SED -i.bck'$ S / $ / yourText2 /'LIST.TXT

通过第二个解决方案,你就会有一个备份过(与首先您需要做手工)。

或者

 前-scS / $ / yourText2 / | W | Q'LIST.TXT

  perl的-i.bck -pe的/ $ / yourText2 /如果EOF'LIST.TXT

How can I add string to the end of the file without line break?

for example if i'm using >> it will add to the end of the file with line break:

cat list.txt
yourText1
root@host-37:/# echo yourText2 >> list.txt
root@host-37:/# cat list.txt
yourText1
yourText2

I would like to add yourText2 right after yourText1

root@host-37:/# cat list.txt
yourText1yourText2

解决方案

sed '$s/$/yourText2/' list.txt > _list.txt_ && mv -- _list.txt_ list.txt

If your sed implementation supports the -i option, you could use:

sed -i.bck '$s/$/yourText2/' list.txt

With the second solution you'll have a backup too (with first you'll need to do it manually).

Alternatively:

ex -sc 's/$/yourText2/|w|q' list.txt 

or

perl -i.bck -pe's/$/yourText2/ if eof' list.txt

这篇关于击:字符串添加到文件末尾,没有断行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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