GNU text utilities - 4. Formatting file contents

Go to the first, previous, next, last section, table of contents.

# 4. Formatting file contents

These commands reformat the contents of files.

## 4.1 `fmt`: Reformat paragraph text

`fmt` fills and joins lines to produce output lines of (at most) a given number of characters (75 by default). Synopsis:

    fmt [option]... [file]...

`fmt` reads from the specified file arguments (or standard input if none are given), and writes to standard output.

By default, blank lines, spaces between words, and indentation are preserved in the output; successive input lines with different indentation are not joined; tabs are expanded on input and introduced on output.

`fmt` prefers breaking lines at the end of a sentence, and tries to avoid line breaks after the first word of a sentence or before the last word of a sentence. A sentence break is defined as either the end of a paragraph or a word ending in any of ``.?!'`, followed by two spaces or end of line, ignoring any intervening parentheses or quotes. Like TeX, `fmt` reads entire "paragraphs" before choosing line breaks; the algorithm is a variant of that in "Breaking Paragraphs Into Lines" (Donald E. Knuth and Michael F. Plass, Software--Practice and Experience, 11 (1981), 1119--1184).

The program accepts the following options. Also see section 2. Common options.

``-c'`
``--crown-margin'`
  Crown margin mode: preserve the indentation of the first two lines within a paragraph, and align the left margin of each subsequent line with that of the second line.
``-t'`
``--tagged-paragraph'`
  Tagged paragraph mode: like crown margin mode, except that if indentation of the first line of a paragraph is the same as the indentation of the second, the first line is treated as a one-line paragraph.
``-s'`
``--split-only'`
  Split lines only. Do not join short lines to form longer ones. This prevents sample lines of code, and other such "formatted" text from being unduly combined.
``-u'`
``--uniform-spacing'`
  Uniform spacing. Reduce spacing between words to one space, and spacing between sentences to two spaces.
``-width'`
``-w width'`
``--width=width'`
  Fill output lines up to width characters (default 75). `fmt` initially tries to make lines about 7% shorter than this, to give it room to balance line lengths.
``-p prefix'`
``--prefix=prefix'`
  Only lines beginning with prefix (possibly preceded by whitespace) are subject to formatting. The prefix and any preceding whitespace are stripped for the formatting and then re-attached to each formatted output line. One use is to format certain kinds of program comments, while leaving the code unchanged.

## 4.2 `pr`: Paginate or columnate files for printing

`pr` writes each file (``-'` means standard input), or standard input if none are given, to standard output, paginating and optionally outputting in multicolumn format; optionally merges all files, printing all in parallel, one per column. Synopsis:

    pr [option]... [file]...

By default, a 5-line header is printed at each page: two blank lines; a line with the date, the filename, and the page count; and two more blank lines. A footer of five blank lines is also printed. With the ``-F'` option, a 3-line header is printed: the leading two blank lines are omitted; no footer is used. The default page_length in both cases is 66 lines. The default number of text lines changes from 56 (without ``-F'`) to 63 (with ``-F'`). The text line of the header takes up the full page_width in the form ``yyyy-mm-dd HH:MM string Page nnnn'`. String is a centered header string.

Form feeds in the input cause page breaks in the output. Multiple form feeds produce empty pages.

Columns are of equal width, separated by an optional string (default is ``space'`). For multicolumn output, lines will always be truncated to page_width (default 72), unless you use the ``-J'` option. For single column output no line truncation occurs by default. Use ``-W'` option to truncate lines in that case.

Including version 1.22i:

Some small letter options (``-s'`, ``-w'`) has been redefined with the object of a better posix compliance. The output of some further cases has been adapted to other unixes. A violation of downward compatibility has to be accepted.

Some new capital letter options (``-J'`, ``-S'`, ``-W'`) has been introduced to turn off unexpected interferences of small letter options. The ``-N'` option and the second argument last_page of ``+FIRST_PAGE'` offer more flexibility. The detailed handling of form feeds set in the input files requires ``-T'` option.

Capital letter options dominate small letter ones.

Some of the option-arguments (compare ``-s'`, ``-S'`, ``-e'`, ``-i'`, ``-n'`) cannot be specified as separate arguments from the preceding option letter (already stated in the posix specification).

The program accepts the following options. Also see section 2. Common options.

``+first_page[:last_page]'`
``--pages=first_page[:last_page]'`
  Begin printing with page first_page and stop with last_page. Missing ``:last_page'` implies end of file. While estimating the number of skipped pages each form feed in the input file results in a new page. Page counting with and without ``+first_page'` is identical. By default, counting starts with the first page of input file (not first page printed). Line numbering may be altered by ``-N'` option.
``-column'`
``--columns=column'`
  With each single file, produce column columns of output (default is 1) and print columns down, unless ``-a'` is used. The column width is automatically decreased as column increases; unless you use the ``-W/-w'` option to increase page_width as well. This option might well cause some lines to be truncated. The number of lines in the columns on each page are balanced. The options ``-e'` and ``-i'` are on for multiple text-column output. Together with ``-J'` option column alignment and line truncation is turned off. Lines of full length are joined in a free field format and ``-S'` option may set field separators. ``-column'` may not be used with ``-m'` option.
``-a'`
``--across'`
  With each single file, print columns across rather than down. The ``-column'` option must be given with column greater than one. If a line is too long to fit in a column, it is truncated.
``-c'`
``--show-control-chars'`
  Print control characters using hat notation (e.g., ``^G'`); print other unprintable characters in octal backslash notation. By default, unprintable characters are not changed.
``-d'`
``--double-space'`
  Double space the output.
``-e[in-tabchar[in-tabwidth]]'`
``--expand-tabs[=in-tabchar[in-tabwidth]]'`
  Expand tabs to spaces on input. Optional argument in-tabchar is the input tab character (default is the TAB character). Second optional argument in-tabwidth is the input tab character's width (default is 8).
``-f'`
``-F'`
``--form-feed'`
  Use a form feed instead of newlines to separate output pages. The default page length of 66 lines is not altered. But the number of lines of text per page changes from default 56 to 63 lines.
``-h HEADER'`
``--header=HEADER'`
  Replace the filename in the header with the centered string header. Left-hand-side truncation (marked by a ``*'`) may occur if the total header line ``yyyy-mm-dd HH:MM HEADER Page nnnn'` becomes larger than page_width. ``-h ""'` prints a blank line header. Don't use ``-h""'`. A space between the ``-h'` option and the argument is always indispensable.
``-i[out-tabchar[out-tabwidth]]'`
``--output-tabs[=out-tabchar[out-tabwidth]]'`
  Replace spaces with tabs on output. Optional argument out-tabchar is the output tab character (default is the TAB character). Second optional argument out-tabwidth is the output tab character's width (default is 8).
``-J'`
``--join-lines'`
  Merge lines of full length. Used together with the column options ``-column'`, ``-a -column'` or ``-m'`. Turns off ``-W/-w'` line truncation; no column alignment used; may be used with ``-S[string]'`. ``-J'` has been introduced (together with ``-W'` and ``-S'`) to disentangle the old (posix compliant) options ``-w'` and ``-s'` along with the three column options.
``-l page_length'`
``--length=page_length'`
  Set the page length to page_length (default 66) lines, including the lines of the header [and the footer]. If page_length is less than or equal 10 (and <= 3 with ``-F'`), the header and footer are omitted, and all form feeds set in input files are eliminated, as if the ``-T'` option had been given.
``-m'`
``--merge'`
  Merge and print all files in parallel, one in each column. If a line is too long to fit in a column, it is truncated, unless ``-J'` option is used. ``-S[string]'` may be used. Empty pages in some files (form feeds set) produce empty columns, still marked by string. The result is a continuous line numbering and column marking throughout the whole merged file. Completely empty merged pages show no separators or line numbers. The default header becomes ``yyyy-mm-dd HH:MM <blanks> Page nnnn'`; may be used with ``-h header'` to fill up the middle blank part.
``-n[number-separator[digits]]'`
``--number-lines[=number-separator[digits]]'`
  Provide digits digit line numbering (default for digits is 5). With multicolumn output the number occupies the first digits column positions of each text column or only each line of ``-m'` output. With single column output the number precedes each line just as ``-m'` does. Default counting of the line numbers starts with 1st line of the input file (not the 1st line printed, compare the ``--page'` option and ``-N'` option). Optional argument number-separator is the character appended to the line number to separate it from the text followed. The default separator is the TAB character. In a strict sense a TAB is always printed with single column output only. The TAB-width varies with the TAB-position, e.g. with the left margin specified by ``-o'` option. With multicolumn output priority is given to ``equal width of output columns'` (a posix specification). The TAB-width is fixed to the value of the 1st column and does not change with different values of left margin. That means a fixed number of spaces is always printed in the place of the number-separator tab. The tabification depends upon the output position.
``-N line_number'`
``--first-line-number=line_number'`
  Start line counting with the number line_number at first line of first page printed (in most cases not the first line of the input file).
``-o margin'`
``--indent=margin'`
  Indent each line with a margin margin spaces wide (default is zero). The total page width is the size of the margin plus the page_width set with the ``-W/-w'` option. A limited overflow may occur with numbered single column output (compare ``-n'` option).
``-r'`
``--no-file-warnings'`
  Do not print a warning message when an argument file cannot be opened. (The exit status will still be nonzero, however.)
``-s[char]'`
``--separator[=char]'`
  Separate columns by a single character char. Default for char is the TAB character without ``-w'` and ``no character'` with ``-w'`. Without ``-s'` default separator ``space'` is set. ``-s[char]'` turns off line truncation of all three column options (``-COLUMN'`|``-a -COLUMN'`|``-m'`) except ``-w'` is set. That is a posix compliant formulation.
``-S[string]'`
``--sep-string[=string]'`
  Use string to separate output columns. The ``-S'` option doesn't affect the ``-W/-w'` option, unlike the ``-s'` option which does. It does not affect line truncation or column alignment. Without ``-S'`, and with ``-J'`, `pr` uses the default output separator, TAB. Without ``-S'` or ``-J'`, `pr` uses a ``space'` (same as ``-S" "'`). Using ``-S'` with no string is equivalent to ``-S""'`. Note that for some of `pr`'s options the single-letter option character must be followed immediately by any corresponding argument; there may not be any intervening white space. ``-S/-s'` is one of them. Don't use ``-S "STRING"'`. POSIX requires this.
``-t'`
``--omit-header'`
  Do not print the usual header [and footer] on each page, and do not fill out the bottom of pages (with blank lines or a form feed). No page structure is produced, but form feeds set in the input files are retained. The predefined pagination is not changed. ``-t'` or ``-T'` may be useful together with other options; e.g.: ``-t -e4'`, expand TAB characters in the input file to 4 spaces but don't make any other changes. Use of ``-t'` overrides ``-h'`.
``-T'`
``--omit-pagination'`
  Do not print header [and footer]. In addition eliminate all form feeds set in the input files.
``-v'`
``--show-nonprinting'`
  Print unprintable characters in octal backslash notation.
``-w page_width'`
``--width=page_width'`
  Set page width to page_width characters for multiple text-column output only (default for page_width is 72). ``-s[CHAR]'` turns off the default page width and any line truncation and column alignment. Lines of full length are merged, regardless of the column options set. No page_width setting is possible with single column output. A posix compliant formulation.
``-W page_width'`
``--page_width=page_width'`
  Set the page width to page_width characters. That's valid with and without a column option. Text lines are truncated, unless ``-J'` is used. Together with one of the three column options (``-column'`, ``-a -column'` or ``-m'`) column alignment is always used. The separator options ``-S'` or ``-s'` don't affect the ``-W'` option. Default is 72 characters. Without ``-W page_width'` and without any of the column options NO line truncation is used (defined to keep downward compatibility and to meet most frequent tasks). That's equivalent to ``-W 72 -J'`. With and without ``-W page_width'` the header line is always truncated to avoid line overflow.

## 4.3 `fold`: Wrap input lines to fit in specified width

`fold` writes each file (``-'` means standard input), or standard input if none are given, to standard output, breaking long lines. Synopsis:

    fold [option]... [file]...

By default, `fold` breaks lines wider than 80 columns. The output is split into as many lines as necessary.

`fold` counts screen columns by default; thus, a tab may count more than one column, backspace decreases the column count, and carriage return sets the column to zero.

The program accepts the following options. Also see section 2. Common options.

``-b'`
``--bytes'`
  Count bytes rather than columns, so that tabs, backspaces, and carriage returns are each counted as taking up one column, just like other characters.
``-s'`
``--spaces'`
  Break at word boundaries: the line is broken after the last blank before the maximum line length. If the line contains no such blanks, the line is broken at the maximum line length as usual.
``-w width'`
``--width=width'`
  Use a maximum line length of width columns instead of 80.

Go to the first, previous, next, last section, table of contents.