我的文本文件只有1行,但有2G字符。
我需要在文件中找到一些内容,但是在任何编辑器中都无法打开该行 - 无论是在Unix上,还是在我将其ftp到我的PC上时......
是否有快速/简单的方法来插入换行符,例如,每120个字符?
答案 0 :(得分:4)
Fold
命令执行此操作,因此如果要在120之后换行..
fold -w120 fie > newfile
答案 1 :(得分:1)
sed 's/\(.\{120\}\)/\1\n/g' file.txt
如果您需要创建新文件,则只需重定向输出:
sed 's/\(.\{120\}\)/\1\n/g' file.txt > new_file.txt
按照每行4个字符的示例
[fotanus@thing ~]$ cat test.txt
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
[fotanus@thing ~]$ sed 's/\(.\{4\}\)/\1\n/g' test.txt > a.txt
[fotanus@thing ~]$ cat a.txt
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
qqqq
答案 2 :(得分:0)
你可以尝试这一行:
sed -r 's/.{120}/&\n/g' file
如果您想将更改写回原始文件:
sed -ir 's/.{120}/&\n/g' file
例如:
文件有100个s
,每12个字符添加一个换行符:
kent$ cat file
ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
kent$ sed -r 's/.{12}/&\n/g' file
ssssssssssss
ssssssssssss
ssssssssssss
ssssssssssss
ssssssssssss
ssssssssssss
ssssssssssss
ssssssssssss
ssss
答案 3 :(得分:0)
如果你不介意创建另一个文件。在bash
上使用linux
对此进行了测试。这里blah.out是输入文件
while read -r -d '' -n 120 record; do echo $record; done<blah.out > blah2.out