Append text to the last line of a file in unix

≯℡__Kan透↙ 提交于 2019-12-12 18:40:17

问题


I want to append a colon character (:) at the end of the last line of a text file (not in a new line).

  • 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.

Is there a better solution?


回答1:


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))


来源:https://stackoverflow.com/questions/50249773/append-text-to-the-last-line-of-a-file-in-unix

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