【问题标题】:extra null characters when sed edit the file in place which under wirtingsed 编辑文件时额外的空字符
【发布时间】:2016-08-24 04:14:24
【问题描述】:

系统:CentOS 6.5

Bash 版本:4.1.2

GNU sed 版本:4.2.1

1.创建正在写入的文件:

$ while sleep 1; do date +ABCDEFG[%d/%b/%Y\ %H\:%M:%S]ABCDEFG;done > access_test.log 2>/dev/null &
$ tail -n 2 access_test.log
  ABCDEFG[29/Apr/2016 14:08:14]ABCDEFG
  ABCDEFG[29/Apr/2016 14:08:15]ABCDEFG 

2.使用sed就地处理。

$ sed -c -i --follow-symlinks -e 'w /dev/stdout' -e 'd' access_test.log > foo
$ less access_test.log
  "access_test.log" may be a binary file.  See it anyway?
  @^@^@^@^@^@^@^@^@^@^@^@^@....<omit>

3.我的问题是:

sed 处理正在写入的文件时,它在完成的那一行留下了很多空(\0)字符。

为什么会这样?我可以避免吗?

来自man sed的备注:

-c, --copy

 use copy instead of rename when shuffling files in -i mode.  While this will avoid breaking links (symbolic
 or hard), the resulting editing operation is not atomic.  This is rarely the  desired  mode;  --follow-sym-
 links is usually enough, and it is both faster and more secure.

【问题讨论】:

  • sed -c -i --follow-symlinks -e 'w /dev/stdout' -e 'd' access_test.log &gt; foo 应该做什么?
  • @fedorqui 将行打印到stdout并就地删除,这样sed不会改变文件的inode号,将其内容保存到其他文件,它的大小会减小。
  • -c 选项是什么,我的 sed 没有?
  • @anubhava 我发现 ubuntu 中的 sed 没有“-c”,但在 centos5/6 中,它有。

标签: linux bash sed centos gnu


【解决方案1】:

当sed处理正在写入的文件时,它在写完的那一行留下了很多空(\0)字符。

为什么??????

这样做是因为另一个进程(while 循环)在写入模式下打开了相同的文件,并且该进程(while 循环)正在将数据写入access_test.log,就在前一个文件指针位置之后。当sed 删除此文件中的所有行时,这会在文件中从文件开始位置到当前文件位置留下空字节 (\0)。

我可以避免吗?????

您应该使用&gt;&gt;(附加模式)重定向而不是使用&gt; 重定向,其中每次写入access_test.log 都将在文件末尾通过将文件指针移动到文件末尾来完成。

这应该可行:

while sleep 1; do date +ABCDEFG[%d/%b/%Y\ %H\:%M:%S]ABCDEFG;done >> access_test.log 2>/dev/null &

【讨论】:

  • 太棒了,是的,就是这样; nginx 日志的模式是“>”还是“>>”模式?我这样做是为了避免向 nginx 发送 sed -USR1 信号,所以我可以使用“sed”方法登录 nginx 吗?
  • 是的,我想你也可以。
猜你喜欢
  • 2023-03-04
  • 1970-01-01
  • 2023-04-07
  • 2022-07-12
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 2012-09-23
相关资源
最近更新 更多