I have files with long text strings that need to be cut to a certain length. This is the code I have done so far:
Now I would like to insert the newline not just after char no. MaxLen but rather after the last space before reaching MaxLen.
I have tried inserting a line
but that does not work and would omit the last few chars.
Any hint is appreciated !
Thanks
Code:
10: MaxLen = 60 # desired length of string
20: LineLen = length($0)
30: if ( length($0) > MaxLen ) { # If $0 > MaxLen :
40: i = int(LineLen / MaxLen) + 1 # i is no. of lines to be written
50: for ( j = 1; j <= i; j++ ) {
60: Pos[j] = ( j * MaxLen ) - MaxLen
70: Line[j] = substr($0,Pos[j],MaxLen)
80: printf("%s\n", Line[j]) > Output
90: }
100: }
110: else printf( "%s\n", $0 ) > Output
Now I would like to insert the newline not just after char no. MaxLen but rather after the last space before reaching MaxLen.
I have tried inserting a line
Code:
71: sub( / $/,"\n",Line[j]) # replace last space with newline
Any hint is appreciated !
Thanks