Bash: add string to the end of the file without line break

后端 未结 4 752
萌比男神i
萌比男神i 2021-02-03 23:28

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         


        
4条回答
  •  梦毁少年i
    2021-02-04 00:23

    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
    

提交回复
热议问题