我有一个只有 1 行但有 2G 个字符的文本文件。
我需要在文件中找到一些东西,但是该行太长而无法在任何编辑器中打开 - 无论是在 Unix 上,还是如果我将其 ftp 到我的 PC...
是否有一种快速/简单的方法来插入换行符,例如每 120 个字符?
我有一个只有 1 行但有 2G 个字符的文本文件。
我需要在文件中找到一些东西,但是该行太长而无法在任何编辑器中打开 - 无论是在 Unix 上,还是如果我将其 ftp 到我的 PC...
是否有一种快速/简单的方法来插入换行符,例如每 120 个字符?
Fold
命令这样做,所以如果你想在 120 之后换行。
fold -w120 fie > newfile
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
你可以试试这条线:
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
如果您不介意创建另一个文件。bash
用on测试了这个linux
。这里 blah.out 是输入文件
while read -r -d '' -n 120 record; do echo $record; done<blah.out > blah2.out