Help with splitting lines in a file using awk

I have a file which is one big long line of text about 10Kb long.
Can someone provide a way using awk to introduce carriage returns every 40 chars in this file.
Any other solutions would also be welcome.

Thank you in advance.

martinbaretto,
If you are using Unix workstation, Open the File in a Text Editor, and check out the option of Word wrap.
Also, if you are using windows, Open Wordpad and select all the text and Format it with wordwrap set.

Then save the file.
Hope this helps.

Anent

This may be hard to do using awk. I think most commands like sed and awk can only operate on lines less then 2048 characters (thats the number that sticks out in my mind, you may have to try it to see what the exact number is). You may want to use Perl, you should be able to read the line into an array, break it up, and spit it back out.

Here is small C program to help you cut the lines to less than 2040 characters per line. Assuming your lines are upto 90000.

# include <stdio.h>
# include <stdlib.h>

main (ARGV,ARGC)
int ARGC;
char *ARGV[];
{
char cmd[90000];
int i;
FILE *fp;
      while (gets(cmd) != (char *)NULL)
      {
        i++;
        if (strlen (cmd) < 2040)
            printf ("%s\n",cmd);
       }
}

HTH, ciao,
Nitin :slight_smile:

added code tags for readability --oombera

The fold command can do this. The only problem is that your input file must have a newline character as the last character. If it doesn't already have one, you can do "echo >> datafile" to add one. Then you can use fold like this:
fold -w40 datafile

Thanks very much everybody.
Perderabo's solution using the fold command seems to be the easiest and fits perfect with my needs.

Martin