josifoski
josifoski

Reputation: 1726

Reformatting text file

I want to reformat one text file, so every paragraph will have approximately 150 characters. After deleting all \n characters we have one long line.

Input:
1 Line

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

Upvotes: 0

Views: 106

Answers (3)

NeronLeVelu
NeronLeVelu

Reputation: 10039

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

128 than 22 due to limitation of posix sed to 128 char per repetition (GNU sed should directly accept 150)

Upvotes: 1

Cyrus
Cyrus

Reputation: 88583

Feel free to try fold:

fold -s -w 150 filename

Upvotes: 2

James Kent
James Kent

Reputation: 5933

you really should post some code of what you've tried on here rather than essentially asking other people to do it for you, but here is a snippet that should do something like what you want and break after the first fullstop:

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

Upvotes: 1

Related Questions