Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cut string to xx char, insert newline at nearest space 2

Status
Not open for further replies.

ChrB

Technical User
Feb 15, 2005
7
CH
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:

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
but that does not work and would omit the last few chars.

Any hint is appreciated !
Thanks
 
Why not using the fold command ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I am using GNU Awk 3.1.0 on Windows and have checked the manual of V. 3.1.3.
There seems to be no fold command... or am I missing something?

thanks
 
Try

BEGIN {maxlen = 60}
{
k = length
while (k>maxlen) {
for (j=maxlen;j>0;j--) if (substr($0,j,1)==" ") break
if (j==0) j = maxlen
print substr($0,1,j)
$0 = substr($0,j+1)
k -= j
}
print
}

CaKiwi
 
CaKiwi - thanks a lot, it works like a charm !
Less code, more functionality !

Am I correctly understanding that the for-loop targets only the statement "if (substr(...) break" ?

Did not know that you can assign a new value to $0 - guess I have learned somic tonight !

Thanks
Chris
 
Yes, the for loop targets only the if (substr($0,j,1)==" ") break

You could write it as

for (j=maxlen;j>0;j--) {
if (substr($0,j,1)==" ") break
}
to make it clearer.

Or as PHV intimated, use

fold -s -60 infile > outfile

CaKiwi
 
Code:
BEGIN { MaxLen=60 }
!NF
{ while ( $0 )
  { s = substr($0,1,MaxLen+1)
    if (length(s)>MaxLen) sub(/ +[^ ]*$/, "", s)
    print s
    $0 = substr($0,length(s)+1);  sub(/^ +/,"")
  }
}
 
I have similar requirement:

I have 3 columns in a pipe seperated file. Second column in this file should be limited to 20 characters. I am using AWK script to read this file and like to cut/fold/anything else within awk....

Please help.
 
A starting point:
awk -F'|' '
{printf "%s|%s|%s\n",$1,substr($2,1,20),$3}
' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top