如何将周期性换行符插入到具有1个令人难以置信的长行的文件中

时间:2013-04-18 18:33:42

标签: unix text-processing

我的文本文件只有1行,但有2G字符。

我需要在文件中找到一些内容,但是在任何编辑器中都无法打开该行 - 无论是在Unix上,还是在我将其ftp到我的PC上时......

是否有快速/简单的方法来插入换行符,例如,每120个字符?

4 个答案:

答案 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