3

我有一个大文件(8GB),我想用 UTF-8 替换前 30 行的 String LATIN1 什么是最有效的方法?意味着存在一种可能使用 sed 但在解析前 30 行后退出的方法。

VIM 无法在 3 小时内保存文件。

4

3 回答 3

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
于 2012-12-24T16:21:28.037 回答
2

With sed, you can quit using q:

sed -e 's/LATIN1/UTF-8/g' -e 30q
于 2012-12-24T16:19:50.690 回答
0

未经测试,但我认为ed会在不写入临时文件的情况下就地编辑文件。

ed yourBigFile << END
1,30s/LATIN1/UTF-8/g
w
q
END
于 2012-12-25T17:15:41.383 回答