sed 编辑文件时的额外空字符
extra null characters when sed edit the file in place which under wirting
System: CentOS 6.5
Bash Version: 4.1.2
GNU sed version: 4.2.1
1.create正在写入的文件:
$ 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.use 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.my问题是:
当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.
when sed deal with the file which is under writing, it leaved lots of null([=19=]) characters which line it finished.
Why ??????
这样做是因为另一个进程(while
循环)在写入模式下打开了相同的文件,并且该进程(while
循环)正在将数据写入 access_test.log
前一个文件指针位置。当 sed
删除此文件中的所有行时,这会在文件中从文件位置开始到当前文件位置留下空字节 ([=14=]
)。
And can i avoid it ?????
而不是使用 >
重定向,您应该使用 >>
(追加模式)重定向,其中每次写入 access_test.log
都将通过将文件指针移动到文件末尾来完成文件结尾。
这应该有效:
while sleep 1; do date +ABCDEFG[%d/%b/%Y\ %H\:%M:%S]ABCDEFG;done >> access_test.log 2>/dev/null &
System: CentOS 6.5
Bash Version: 4.1.2
GNU sed version: 4.2.1
1.create正在写入的文件:
$ 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.use 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.my问题是:
当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.
when sed deal with the file which is under writing, it leaved lots of null([=19=]) characters which line it finished.
Why ??????
这样做是因为另一个进程(while
循环)在写入模式下打开了相同的文件,并且该进程(while
循环)正在将数据写入 access_test.log
前一个文件指针位置。当 sed
删除此文件中的所有行时,这会在文件中从文件位置开始到当前文件位置留下空字节 ([=14=]
)。
And can i avoid it ?????
而不是使用 >
重定向,您应该使用 >>
(追加模式)重定向,其中每次写入 access_test.log
都将通过将文件指针移动到文件末尾来完成文件结尾。
这应该有效:
while sleep 1; do date +ABCDEFG[%d/%b/%Y\ %H\:%M:%S]ABCDEFG;done >> access_test.log 2>/dev/null &