[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4. Formatting file contents

These commands reformat the contents of files.

4.1 fmt: Reformat paragraph text  Reformat paragraph text.
4.2 pr: Paginate or columnate files for printing  Paginate or columnate files for printing.
4.3 fold: Wrap input lines to fit in specified width  Wrap input lines to fit in specified width.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

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 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.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

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 the form `date string page', with spaces inserted around string so that the line takes up the full page_width. Here, date is the date (see the `-D' or `--date-format' option for details), string is the centered header string, and page identifies the page number. The LC_MESSAGES locale category affects the spelling of page; in the default C locale, it is `Page number' where number is the decimal page number.

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.

The following changes were made in version 1.22i and apply to later versions of pr:  - Brian

The program accepts the following options. Also see 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 nonprinting characters in octal backslash notation. By default, nonprinting characters are not changed.

`-d'
`--double-space'
Double space the output.

`-D format'
`--date-format=format'
Format header dates using format, using the same conventions as for the the command `date +format'; See section 21.1 date: Print or set system date and time. Except for directives, which start with `%', characters in format are printed unchanged. You can use this option to specify an arbitrary string in place of the header date, e.g., `--date-format="Monday morning"'.

If the POSIXLY_CORRECT environment variable is not set, the date format defaults to `%Y-%m-%d %H:%M' (for example, `2001-12-04 23:59'); otherwise, the format depends on the LC_TIME locale category, with the default being `%b %e %H:%M %Y' (for example, `Dec  4 23:59 2001'.

`-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. When using the shell, header should be quoted and should be separated from `-h' by a space.

`-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 `--sep-string[=string]'. `-J' has been introduced (together with `-W' and `--sep-string') 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 to 10 (or <= 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 the `-J' option is used. `--sep-string[=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 `date page' with spaces inserted in the middle; this may be used with the `-h' or `--header' option 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 the first line of the input file (not the first 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 first 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. The default for char is the TAB character without `-w' and `no character' with `-w'. Without `-s' the default separator `space' is set. `-s[char]' turns off line truncation of all three column options (`-COLUMN'|`-a -COLUMN'|`-m') unless `-w' is set. This 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" "'). With `-Sstring', string must be nonempty; `--sep-string' with no string is equivalent to `--sep-string=""'.

On older systems, pr instead supports an obsolete option `-S[string]', where string is optional. POSIX 1003.1-2001 (see section 2.5 Standards conformance) does not allow this older usage. To specify an empty string portably, use `--sep-string'.

`-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 nonprinting 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'. The header line is never truncated.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

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 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.

On older systems, fold supports an obsolete option `-width'. POSIX 1003.1-2001 (see section 2.5 Standards conformance) does not allow this; use `-w width' instead.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Jeff Bailey on December, 28 2002 using texi2html