Unwanted line break using echo and cat

陌路散爱 提交于 2019-12-02 14:01:19

问题


I'm trying to add a line at the beginning of a file, using

echo 'time/F:x1:x2' | cat - file.txt>newfile.txt

But this produces line breaks at each line in the new file (except for after the added 'time/F:x1:x2' line). Any ideas on how to avoid this?


回答1:


Use -n to disable the trailing newline:

echo -n 'time/F:x1:x2' | cat - file.txt > newfile.txt

There are other ways, too:

sed '1s|^|time/F:x1:x2|' file.txt > newfile.txt



回答2:


How about

{ echo 'time/F:x1:x2'; cat file.txt; } >newfile.txt

or

sed '1i\
time/F:x1:x2' file.txt > newfile.txt



回答3:


Actually you don't even need the echo and pipe if you're using bash. Just use a herestring:

<<< 'time/F:x1:x2' cat - file.txt > newfile.txt


来源:https://stackoverflow.com/questions/22487023/unwanted-line-break-using-echo-and-cat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!