【问题标题】:Replace last character in a large file (whole file is a single line)替换大文件中的最后一个字符(整个文件是单行)
【发布时间】:2019-12-01 10:02:15
【问题描述】:

我有一个单行的 60GB 文件。

我需要做的就是更改最后一个“,”(文件中的最后一个字符)。

问题是sed 无法处理它,因为它都在一行中并且无法分配内存。

// file.txt
[0] ...mple12,sample13),(sample21,sample22,sample23),

// desired file.txt
[0] ...mple12,sample13),(sample21,sample22,sample23);

我收到一个错误Couldn't re-allocate memory

【问题讨论】:

  • 您可以在一个编辑器中打开它并进行修改。如果这只是一份一次性工作。
  • 它实际上是作为管道的一部分经常重复的东西

标签: linux text sed character


【解决方案1】:

在这种情况下,面向流的方法可能会有所帮助。 可以用shell轻松实现:

# First remove last character
head -c -1 < file.txt > file2.txt
# then add new last character ';' to the end
echo -n ";" >> file2.txt

请注意:如果文件末尾有 CR,则需要使用 'head -c -2' 代替。

单行是:

head -c -1 <file.txt | (cat - ; echo ';') > file2.txt

【讨论】:

  • 好答案!只是提醒 O.P. 你需要有 2 个 60GB 文件的空间,你不能重定向回你的原始文件名,因为这会删除文件。即不要.....) &gt; file1.txt。祝大家好运。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-11
  • 2018-07-09
相关资源
最近更新 更多