在 unix 中将文本附加到文件的最后一行 [英] Append text to the last line of a file in unix

查看:21
本文介绍了在 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 ":" >>file 将冒号置于新行中.
  • 使用 sed '$s/$/:/' 文件 >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.

有更好的解决方案吗?

推荐答案

您可以使用 ddnotrunc(在 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屋!

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