【发布时间】: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 > foo应该做什么? -
@fedorqui 将行打印到stdout并就地删除,这样sed不会改变文件的inode号,将其内容保存到其他文件,它的大小会减小。
-
-c选项是什么,我的 sed 没有? -
@anubhava 我发现 ubuntu 中的 sed 没有“-c”,但在 centos5/6 中,它有。