Splitting a single line into multiple lines

I have a case where, I need to look into a file.
Go to each line of the file, find the length of the line, if the length of the line is more than 75 chars, I need to split the line into multiple lines of 75chars max. If the length of the line is less than 75, we need not do anything.
So at the end of this operation , maximum length of anyline in the file should be only 75.

Ex: file having 3 lines
1st line: 20chars
2nd line: 85chars
3rd line: 160chars

after the executing the script,
The file should be

1st line:20 chars

2nd line: 75 chars
3rd line: 10 chars

4th line: 75 chars
5th line: 75 chars
6th line: 10 chars

Thanks for your help

Try the following ....

But the code is tested with 5 chars as criterion instead of 75.
Change as per your requirement.

awk '{
  if ( length($0) > 5 )
  {
      str=$0 ;
      i=0 ;
      while(length(str) > 5)
      {
        printf("%s\n",substr($0,i+1,5) );
        i+=5 ;
        str=substr($0,i,length($0) );
      }

      if( length(str) > 1 )
        printf("%s\n", substr(str,2,length(str))) ;

  }
  else
  {
      print $0
  }
}' file1

Or...

fold -75 file1 > file2

fold -w 5 file1 > file2

good one Ygor ! I prefer this one compared to the long one i wrote.:slight_smile:

Thanks for your responses. It's working great...