REFORMAT
Rewrite a Text File With Fixed Number of Words Per Line


REFORMAT is a FORTRAN90 program which reads an ASCII text file, and writes a copy, with the property that every line (except perhaps the last one) has the same number of values on it.

This program was "inspired" by a situation in which the data in a file was not arranged as expected.

It was assumed that there were 4 items in every line.

But in fact, a free format had been used, and the system "felt free" to write 3 values per line, resulting in a ragged file of lines of alternating lengths of 3 and 1.

One can easily imagine other cases where 11 values would be written as 3/3/3/2.

And if the system had used another limit for the number of values per line, we would find that L values had been written as M/M/M/M/N instead.

The purpose of this program is allow for the repair of these "mistakes" in the simplest case. Thus, we imagine the command


        reformat ragged.txt smooth.txt 4
      
would read data from the file "ragged.txt", and write it out 4 words at a time.

In my experience, it has always been the case that several lines of the ragged file always exactly equaled one line of the smooth file. But it is easy to imagine more complicated situations.

The reasonable thing to do, I think, is to interpret the command as an attempt to get ALL the information in the ragged file into the smooth file, using the fixed limit.

This decision means the program can also be used to take a file of four words per line and turn it into one of 3 words per line.

This version of the program is only able to deal with files that contain nothing but real numbers. For a version that works with abstract 'words', see REWORD below.

Usage:

reformat input output length
where

Licensing:

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

Related Data and Programs:

CRRM, a C++ program which reads a text file and writes a copy which has no carriage returns.

DEBLANK, a C++ program which reads a text file and writes a copy which has no blank lines.

DECOMMENT, a C++ program which makes a copy of a text file which contains no "comment" lines (that begin with "#").

FILE_TRANSPOSE, a FORTRAN90 program which makes a "transposed" copy of a file, in which the I-th "word" of the J-th line becomes the J-th word of the I-th line.

FILUM, a FORTRAN90 library which performs various operations on files.

REWORD, a C++ program which reads a text file and writes a copy which has a fixed number of "words" per line.

TABLE_COLUMNS, a FORTRAN90 program which will extract the data in certain columns of a file.

UNCONTROL, a C++ program which makes a copy of a text file which contains no control characters.

WRAP, a C++ program which makes a copy of a text file in which no line is longer than a user-specified wrap length.

WRAP2, a C++ program which wraps long lines in a text file, but which wraps some lines "early", so as to avoid breaking words.

Source Code:

Examples and Tests:

List of Routines:

You can go up one level to the FORTRAN90 source codes.


Last revised on 12 December 2005.