在UNIX中将文本追加到文件的最后一行 [英] Append text to the last line of a file in unix
本文介绍了在UNIX中将文本追加到文件的最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在文本文件的最后一行末尾添加冒号(:)(不在新行中)。
I want to append a colon character (:) at the end of the last line of a text file (not in a new line).
- 我的文件末尾已经有一个\n字符,因此
printf:>>文件
将冒号放在新行中。 - 使用
sed‘$ s / $ /:/’file> newfile
可以工作,但是我的文件约为100 MB,因此,用管道输送整个内容以添加单个字符似乎没有吸引力。
- My file already has a \n character at the end so
printf ":" >> file
puts the colon in a new line. - Using
sed '$s/$/:/' file > newfile
works, but my file is ~100 MB so piping the whole thing just to add a single character seems unattractive.
有更好的解决方案吗?
推荐答案
您可以选择 dd
和 notrunc
(在Linux 4.12上测试):
You could go with dd
and notrunc
(tested on Linux 4.12):
printf ":" | dd of=file conv=notrunc bs=1 seek=$(( $(stat -c "%s" file) - 1))
这篇关于在UNIX中将文本追加到文件的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文