重新格式化文本文件

时间:2014-10-03 08:51:32

标签: python awk sed

我想重新格式化一个文本文件,因此每个段落大约有150个字符。 删除所有\ n字符后,我们有一个长行。

Input:
1 Line

Output:
Every first blank after every 150 characters to be replaced with \n

3 个答案:

答案 0 :(得分:2)

随意尝试折叠:

fold -s -w 150 filename

答案 1 :(得分:1)

你真的应该发布一些你在这里尝试过的代码,而不是基本上要求其他人为你做这些代码,但是这里有一个片段应该做你想要的东西并在第一个完整停止后中断:< / p>

inputline = "somelongstring"
outputline = ""

count = 0

for character in inputline: #iterate through the line
    count += 1 #increment the counter on each loop
    if count >= 150: #check counter
        if character == ".": #if fullstop then add fullstop and newline to output
            outputline += ".\n"
            count = 0 #reset counter
        else:
            outputline += character #otherwise pass character to output
    else:
        outputline += character #otherwise pass character to output

答案 2 :(得分:1)

sed 's/\(.\{128\}.\{22\}[^ ]*\) /\1\
/g' YourFile

128比22由于posix sed限制为每次重复128个字符(GNU sed应该直接接受150)

相关问题