我有一个大文件(8GB),我想用 UTF-8 替换前 30 行的 String LATIN1 什么是最有效的方法?意味着存在一种可能使用 sed 但在解析前 30 行后退出的方法。
VIM 无法在 3 小时内保存文件。
The problem is that in the event of a replacement, all programs will make a copy of the file with the substitution in place in order to replace the original file ultimately -- they don't want to risk losing the original for obvious reasons.
With perl, you can do this in a one-liner, but that doesn't make it any shorter (well, it probably does compared to vim, since vim preserves history in yet another file, which perl doesn't):
perl -pi -e 's,\bLATIN1\b,UTF-8,g if $. <= 30' thefile
With sed
, you can quit using q
:
sed -e 's/LATIN1/UTF-8/g' -e 30q
未经测试,但我认为ed
会在不写入临时文件的情况下就地编辑文件。
ed yourBigFile << END
1,30s/LATIN1/UTF-8/g
w
q
END