linux - 替换大文件中的最后一个字符(整个文件是一行)

标签 linux text sed character

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

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

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

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

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

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

最佳答案

在这种情况下,面向流的方法可能会有所帮助。 可以很容易地用 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

关于linux - 替换大文件中的最后一个字符(整个文件是一行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57162770/

相关文章:

C++ :How to navigate to a particular line in a file while writing in a file?

linux - 使用 tar 命令将文件拆分为特定大小

ios - Swift 3 - 向 UIView 弹出窗口添加文本

text - 如何获取包含特定 url 的 <a> 标签中的文本

regex - sed 和 awk 正则表达式有什么区别

linux - linux "clock()"函数是否计入进程的 "sleep"时间?

Linux Vim 删除不包含 abc 或 xyz 的行

text - 命名实体作为文本分类中的一个特征?

Linux搜索和替换一个复杂的字符串

regex - 帮助 SED 语法 : unterminated `s' command