1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
|
@c $Id: formatting-text.texinfo,v 1.22 2001/06/04 15:29:04 m Exp m $
@node Formatting Text, Searching Text, Analyzing Text, Text
@comment node-name, next, previous, up
@chapter Formatting Text
@cindex formatting text
@cindex text, formatting
@noindent
Methods and tools for changing the arrangement or presentation of text
are often useful for preparing text for printing. This chapter discusses
ways of changing the spacing of text and setting up pages, of
underlining and sorting and reversing text, and of numbering lines of
text.
@menu
* Spacing Text:: Change the spacing in text.
* Paginating Text:: Paginating text.
* Underlining Text:: Underlining text.
* Sorting Text:: Sorting text.
* Numbering Text:: Numbering text.
* Reversing Text:: Reversing text.
@end menu
@node Spacing Text, Paginating Text, Formatting Text, Formatting Text
@comment node-name, next, previous, up
@section Spacing Text
@cindex spacing text
@cindex text, spacing
@noindent
These recipes are for changing the @emph{spacing} of text---the
whitespace that exists between words, lines, and paragraphs.
The filters described in this section send output to standard output by
default; to save their output to a file, use shell redirection
(@pxref{Standard Output, , Redirecting Output to a File}).
@menu
* Uniform Whitespace:: Making the whitespace the same.
* Single-Spacing:: Single-spacing text.
* Double-Spacing:: Double-spacing text.
* Triple-Spacing:: Triple-spacing text.
* Line Breaks:: Putting line breaks in text.
* Adding Margins:: Putting margins in text.
* Swapping Tabs:: Swapping tab and space characters.
@end menu
@node Uniform Whitespace, Single-Spacing, Spacing Text, Spacing Text
@comment node-name, next, previous, up
@subsection Eliminating Extra Spaces in Text
@cindex eliminating extra spaces in text
@cindex spaces, eliminating extra in text
@cindex text, eliminating extra spaces in
@pindex fmt
@pindex cat
@noindent
To eliminate extra whitespaces @code{within} lines of text, use the
@code{fmt} filter; to eliminate extra whitespace @emph{between} lines of
text, use @code{cat}.
Use @code{fmt} with the @samp{-u} option to output text with ``uniform
spacing,'' where the space between words is reduced to one space
character and the space between sentences is reduced to two space
characters.
@itemize @bullet
@item
To output the file @file{term-paper} with uniform spacing, type:
@example
$ @kbd{fmt -u term-paper @key{RET}}
@end example
@end itemize
Use @code{cat} with the @samp{-s} option to ``squeeze'' multiple
adjacent blank lines into one.
@itemize @bullet
@item
To output the file @file{term-paper} with multiple blank lines output as
only one blank line, type:
@example
$ @kbd{cat -s term-paper @key{RET}}
@end example
@end itemize
You can combine both of these commands to output text with multiple
adjacent lines removed @emph{and} give it a unified spacing between
words. The following example shows how the output of the combined
commands is sent to @code{less} so that it can be perused on the screen.
@itemize @bullet
@item
To peruse the text file @file{term-paper} with multiple blank lines
removed and giving the text unified spacing between words, type:
@example
$ @kbd{cat -s term-paper | fmt -u | less @key{RET}}
@end example
@end itemize
Notice that in this example, both @code{fmt} and @code{less} worked on
their standard input instead of on a file---the standard output of
@code{cat} (the contents of @file{term-paper} with extra blank lines
squeezed out) was passed to the standard input of @code{fmt}, and its
standard output (the space-squeezed @file{term-paper}, now with uniform
spacing) was sent to the standard input of @code{less}, which displayed
it on the screen.
@node Single-Spacing, Double-Spacing, Uniform Whitespace, Spacing Text
@comment node-name, next, previous, up
@subsection Single-Spacing Text
@cindex single-spacing text
@cindex text, single-spacing
@pindex grep
@pindex sed
@noindent
There are many methods for single-spacing text. To remove all empty
lines from text output, use @code{grep} with the regular expression
@samp{.}, which matches any character, and therefore matches any line
that isn't empty (@pxref{Regexps, , Regular Expressions---Matching Text
Patterns}). You can then redirect this output to a file, or pipe it to
other commands; the original file is not altered.
@itemize @bullet
@item
To output all non-empty lines from the file @file{term-paper}, type:
@example
$ @kbd{grep . term-paper @key{RET}}
@end example
@end itemize
This command outputs all lines that are not empty---so lines containing
only non-printing characters, such as spaces and tabs, will still be
output.
To remove from the output all empty lines, and all lines that consist of
only space characters, use @samp{[^ ].} as the regexp to search for. But
this regexp will still output lines that contain only tab characters; to
remove from the output all empty lines and lines that contain only a
combination of tab or space characters, use @samp{[^[:space:]].} as the
regexp to search for. It uses the special predefined @samp{[:space:]}
regexp class, which matches any kind of space character at all,
including tabs.
@itemize @bullet
@item
To output only the lines from the file @file{term-paper} that contain
more than just space characters, type:
@example
$ @kbd{grep '[^ ].' term-paper @key{RET}}
@end example
To output only the lines from the file @file{term-paper} that contain
more than just space or tab characters, type:
@example
$ @kbd{grep '[^[:space:]].' term-paper @key{RET}}
@end example
@end itemize
If a file is already double-spaced, where all even lines are blank, you
can remove those lines from the output by using @code{sed} with the
@samp{n;d} expression.
@itemize @bullet
@item
To output only the odd lines from file @file{term-paper}, type:
@example
$ @kbd{sed 'n;d' term-paper @key{RET}}
@end example
@end itemize
@node Double-Spacing, Triple-Spacing, Single-Spacing, Spacing Text
@comment node-name, next, previous, up
@subsection Double-Spacing Text
@cindex double-spacing text
@cindex text, double-spacing
@pindex pr
@noindent
To double-space text, where one blank line is inserted between each line
in the original text, use the @code{pr} tool with the @samp{-d}
option. By default, @code{pr} paginates text and puts a header at the
top of each page with the current date, time, and page number; give the
@samp{-t} option to omit this header.
@itemize @bullet
@item
To double-space the file @file{term-paper} and write the output to the
file @file{term-paper.print}, type:
@example
$ @kbd{pr -d -t term-paper > term-paper.print @key{RET}}
@end example
@end itemize
To send the output directly to the printer for printing, you would pipe
the output @w{to @code{lpr}:}
@example
$ @kbd{pr -d -t term-paper | lpr @key{RET}}
@end example
@sp .25
@noindent
@strong{NOTE:} The @code{pr} (``print'') tool is a text pre-formatter,
often used to paginate and otherwise prepare text files for printing;
there is more discussion on the use of this tool in @ref{Paginating
Text, , Paginating Text}.
@node Triple-Spacing, Line Breaks, Double-Spacing, Spacing Text
@comment node-name, next, previous, up
@subsection Triple-Spacing Text
@cindex triple-spacing text
@cindex text, triple-spacing
@pindex sed
@noindent
To triple-space text, where two blank lines are inserted between each
line of the original text, use @code{sed} with the @samp{'G;G'}
expression.
@itemize @bullet
@item
To triple-space the file @file{term-paper} and write the output to the
file @file{term-paper.print}, type:
@example
$ @kbd{sed 'G;G' term-paper > term-paper.print @key{RET}}
@end example
@end itemize
The @samp{G} expression appends one blank line to each line of
@code{sed}'s output; using @samp{;} you can specify more than one blank
line to append (but you must quote this command, because the semicolon
(@samp{;}) has meaning to the shell---see @ref{Quoting, , Passing
Special Characters to Commands}). You can use multiple @samp{G}
characters to output text with more than double or triple spaces.
@itemize @bullet
@item
To quadruple-space the file @file{term-paper}, and write the output to the
file @file{term-paper.print}, type:
@example
$ @kbd{sed 'G;G;G' term-paper > term-paper.print @key{RET}}
@end example
@end itemize
The usage of @code{sed} is described in @ref{Stream Editing, , Editing
Streams of Text}.
@node Line Breaks, Adding Margins, Triple-Spacing, Spacing Text
@comment node-name, next, previous, up
@subsection Adding Line Breaks to Text
@cindex adding line breaks to text
@cindex text, adding line breaks to
@cindex line breaks, adding to text
@pindex fmt
@noindent
Sometimes a file will not have line breaks at the end of each line (this
commonly happens during file conversions between operating systems). To
add line breaks to a file that does not have them, use the text
formatter @code{fmt}. It outputs text with lines arranged up to a
specified width; if no length is specified, it formats text up to a
width of 75 characters per line.
@itemize @bullet
@item
To output the file @file{term-paper} with lines up to 75 characters
long, type:
@example
$ @kbd{fmt term-paper @key{RET}}
@end example
@end itemize
Use the @samp{-w} option to specify the maximum line width.
@itemize @bullet
@item
To output the file @file{term-paper} with lines up to 80 characters
long, type:
@example
$ @kbd{fmt -w 80 term-paper @key{RET}}
@end example
@end itemize
@node Adding Margins, Swapping Tabs, Line Breaks, Spacing Text
@comment node-name, next, previous, up
@subsection Adding Margins to Text
@cindex adding margins to text
@cindex text, adding margins to
@cindex margins, adding to text
@pindex pr
@pindex lpr
@noindent
Giving text an extra left margin is especially good when you want to
print a copy and punch holes in it for use with a three-ring binder.
To output a text file with a larger left margin, use @code{pr} with the
file name as an argument; give the @samp{-t} option (to disable headers
and footers), and, as an argument to the @samp{-o} option, give the
number of spaces to offset the text. Add the number of spaces to the
page width (whose default is 72) and specify this new width as an
argument to the @samp{-w} option.
@itemize @bullet
@item
To output the file @file{owners-manual} with a five-space (or
five-@emph{column}) margin to a new file, @file{owners-manual.pr}, type:
@example
$ @kbd{pr -t -o 5 -w 77 owners-manual > owners-manual.pr @key{RET}}
@end example
@end itemize
This command is almost always used for printing, so the output is
usually just piped to @code{lpr} instead of saved to a file. Many text
documents have a width of 80 and not 72 columns; if you are printing
such a document and need to keep the 80 columns across the page, specify
a new width of 85. If your printer can only print 80 columns of text,
specify a width of 80; the text will be reformatted to 75 columns after
the 5-column margin.
@itemize @bullet
@item
To print the file @file{owners-manual} with a 5-column margin and 80
columns of text, type:
@example
$ @kbd{pr -t -o 5 -w 85 owners-manual | lpr @key{RET}}
@end example
@item
To print the file @file{owners-manual} with a 5-column margin and 75
columns of text, type:
@example
$ @kbd{pr -t -o 5 -w 80 owners-manual | lpr @key{RET}}
@end example
@end itemize
@node Swapping Tabs, , Adding Margins, Spacing Text
@comment node-name, next, previous, up
@subsection Swapping Tab and Space Characters
@cindex swapping tab and space characters
@cindex tab and space characters, swapping
@cindex space and tab characters, swapping
@pindex expand
@pindex unexpand
@noindent
Use the @code{expand} and @code{unexpand} tools to swap tab characters
for space characters, and to swap space characters with tabs,
respectively.
Both tools take a file name as an argument and write changes to the
standard output; if no files are specified, they work on the standard
input.
To convert tab characters to spaces, use @code{expand}. To convert only
the @emph{initial} or leading tabs on each line, give the @samp{-i}
option; the default action is to convert @emph{all} tabs.
@itemize @bullet
@item
To convert all tab characters to spaces in file @file{list}, and
write the output to @file{list2}, type:
@example
$ @kbd{expand list > list2 @key{RET}}
@end example
@item
To convert only initial tab characters to spaces in file @file{list},
and write the output to the standard output, type:
@example
$ @kbd{expand -i list @key{RET}}
@end example
@end itemize
To convert multiple space characters to tabs, use @code{unexpand}. By
default, it only converts leading spaces into tabs, counting eight space
characters for each tab. Use the @samp{-a} option to specify that
@emph{all} instances of eight space characters be converted to tabs.
@itemize @bullet
@item
To convert every eight leading space characters to tabs in file
@file{list2}, and write the output to @file{list}, type:
@example
$ @kbd{unexpand list2 > list @key{RET}}
@end example
@item
To convert all occurrences of eight space characters to tabs in file
@file{list2}, and write the output to the standard output, type:
@example
$ @kbd{unexpand -a list2 @key{RET}}
@end example
@end itemize
To specify the number of spaces to convert to a tab, give that number as
an argument to the @samp{-t} option.
@itemize @bullet
@item
To convert every leading space character to a tab character in
@file{list2}, and write the output to the standard output, type:
@example
$ @kbd{unexpand -t 1 list2 @key{RET}}
@end example
@end itemize
@node Paginating Text, Underlining Text, Spacing Text, Formatting Text
@comment node-name, next, previous, up
@section Paginating Text
@cindex paginating text
@cindex text, paginating
@pindex pr
@pindex Emacs
@noindent
The formfeed character, ASCII @kbd{C-l} or octal code 014, is the
delimiter used to paginate text. When you send text with a formfeed
character to the printer, the current page being printed is ejected and
a new page begins---thus, you can paginate a text file by inserting
formfeed characters at a place where you want a page break to occur.
To insert formfeed characters in a text file, use the @code{pr} filter.
Give the @samp{-f} option to omit the footer and separate pages of
output with the formfeed character, and use @samp{-h ""} to output a
blank header (otherwise, the current date and time, file name, and
current page number are output at the top of each page).
@itemize @bullet
@item
To paginate the file @file{listings} and write the output to a file
called @file{listings.page}, type:
@example
$ @kbd{pr -f -h "" listings > listings.page @key{RET}}
@end example
@end itemize
By default, @code{pr} outputs pages of 66 lines each. You can specify
the page length as an argument to the @samp{-l} option.
@itemize @bullet
@item
To paginate the file @file{listings} with 43-line pages, and write the
output to a file called @file{listings.page}, type:
@example
$ @kbd{pr -f -h "" -l 43 listings > listings.page @key{RET}}
@end example
@end itemize
@sp .25
@noindent
@strong{NOTE:} If a page has more lines than a printer can fit on a
physical sheet of paper, it will automatically break the text at that
line as well as at the places in the text where there are formfeed
characters.
You can paginate text in Emacs by manually inserting formfeed characters
where you want them---see @ref{Emacs Inserts, , Inserting Special
Characters in Emacs}.
@menu
* Pr Headers:: Putting headers on a page.
* Pr Columns:: Putting text in columns.
* Pr Options:: More options for pagination.
@end menu
@node Pr Headers, Pr Columns, Paginating Text, Paginating Text
@comment node-name, next, previous, up
@subsection Placing Headers on Each Page
@cindex placing headers on each page
@cindex headers, placing on text pages
@pindex pr
@noindent
The @code{pr} tool is a general-purpose page formatter and
print-preparation utility. By default, @code{pr} outputs text in pages
of 66 lines each, with headers at the top of each page containing the
date and time, file name, and page number, and footers containing five
blank lines.
@itemize @bullet
@item
To print the file @file{duchess} with the default @code{pr} preparation,
type:
@example
$ @kbd{pr duchess | lpr @key{RET}}
@end example
@end itemize
@node Pr Columns, Pr Options, Pr Headers, Paginating Text
@comment node-name, next, previous, up
@subsection Placing Text in Columns
@cindex placing text in columns
@cindex text, placing in columns
@cindex columns, placing text in
@pindex pr
@noindent
You can also use @code{pr} to put text in columns---give the number of
columns to output as an argument. Use the @samp{-t} option to omit the
printing of the default headers and footers.
@itemize @bullet
@item
To print the file @file{news.update} in four columns with no headers or
footers, type:
@example
$ @kbd{pr -4 -t news.update | lpr @key{RET}}
@end example
@end itemize
@node Pr Options, , Pr Columns, Paginating Text
@comment node-name, next, previous, up
@subsection Options Available When Paginating Text
@cindex options available when paginating text
@cindex paginating text, options available when
@pindex pr
@noindent
The following table describes some of @code{pr}'s options; see the
@code{pr} @code{info} for a complete description of its capabilities
(@pxref{Info, , Using the GNU Info System}).
@multitable @columnfractions .30 .70
@item @sc{Option}
@tab @sc{Description}
@item @code{+@var{first}:@var{last}}
@tab Specify the first and last page to process; the last page can be
omitted, so @kbd{+7} begins processing with the seventh page and
continues until the end of the file is reached.
@item @code{-@var{column}}
@tab Specify the number of columns to output text in, making all columns
fit the page width.
@item @code{-a}
@tab Print columns across instead of down.
@item @code{-c}
@tab Output control characters in hat notation and print all other
unprintable characters in ``octal backslash'' notation.
@item @code{-d}
@tab Specify double-spaced output.
@item @code{-f}
@tab Separate pages of output with a formfeed character instead of a
footer of blank lines (63 lines of text per 66-line page instead of 53).
@item @code{-h @var{header}}
@tab Specify the header to use instead of the default; specify @kbd{-h
""} for a blank header.
@item @code{-l @var{length}}
@tab Specify the page length to be @var{length} lines (default 66). If
page length is less than 11, headers and footers are omitted and
existing form feeds are ignored.
@item @code{-m}
@tab Use when specifying multiple files; this option merges and outputs
them in parallel, one per column.
@item @code{-o @var{spaces}}
@tab Set the number of spaces to use in the left margin (default 0).
@item @code{-t}
@tab Omit the header and footer on each page, but retain existing
formfeeds.
@item @code{-T}
@tab Omit the header and footer on each page, as well as existing
formfeeds.
@item @code{-v}
@tab Output non-printing characters in ``octal backslash'' notation.
@item @code{-w @var{width}}
@tab Specify the page width to use, in characters (default 72).
@end multitable
@sp .25
@noindent
@strong{NOTE:} It's also common to use @code{pr} to change the spacing
of text (@pxref{Spacing Text, , Spacing Text}).
@node Underlining Text, Sorting Text, Paginating Text, Formatting Text
@comment node-name, next, previous, up
@section Underlining Text
@cindex underlining text
@cindex text, underlining
@cindex Emacs
@cindex TeX{}
@cindex LaTeX
@pindex ul
@pindex col
@pindex emacs
@noindent
In the days of typewriters, text that was meant to be set in an
italicized font was denoted by underlining the text with underscore
characters; now, it's common practice to denote an italicized word in
plain text by typing an underscore character, @samp{_}, just before and
after a word in a text file, like @samp{_this_}.
Some text markup languages use different methods for denoting italics;
for example, in @TeX{} or LaTeX files, italicized text is often denoted
with brackets and the @samp{\it} command, like @samp{@{\it this@}}.
(LaTeX files use the same format, but @samp{\emph} is often used in
place of @samp{\it}.)
You can convert one form to the other by using the Emacs
@code{replace-regular-expression} function and specifying the text to be
replaced as a regexp (@pxref{Regexps, , Regular Expressions---Matching
Text Patterns}).
@itemize @bullet
@item
To replace plaintext-style italics with @TeX{} @samp{\it} commands, type:
@example
@kbd{ M-x replace-regular-expression @key{RET}}
@kbd{_\([^_]+\)_ @key{RET}}
@kbd{\@{\\it \1@} @key{RET}}
@end example
@item
To replace @TeX{}-style italics with plaintext _underscores_, type:
@example
@kbd{ M-x replace-regular-expression @key{RET}}
@kbd{\@{\\it \@{\([^\@}]+\)\@} @key{RET}}
@kbd{_\1_ @key{RET}}
@end example
@end itemize
Both examples above used the special regexp symbol @samp{\1}, which
matches the same text matched by the first @samp{\( ... \)} construct in
the previous regexp. @inforef{Regexps, The GNU Emacs Manual,
emacs-e20.info} for more information on regexp syntax in Emacs.
To put a literal underline under text, you need to use a text editor to
insert a @code{C-h} character followed by an underscore (@samp{_})
immediately after each character you want to underline; you can insert
the @code{C-h} in Emacs with the @kbd{C-q} function (@pxref{Emacs
Inserts, , Inserting Special Characters in Emacs}).
When a text file contains these literal underlines, use the @code{ul}
tool to output the file so that it is viewable by the terminal you are
using; this is also useful for printing (pipe the output of @code{ul} to
@code{lpr}).
@itemize @bullet
@item
To output the file @file{term-paper} so that you can view underbars, type:
@example
$ @kbd{ul term-paper @key{RET}}
@end example
@end itemize
To output such text without the backspace character, @kbd{C-h}, in the
output, use @code{col} with the @samp{-u} option.
@itemize @bullet
@item
To output the file @file{term-paper} with all backspace characters
stripped out, type:
@example
$ @kbd{col -u term-paper @key{RET}}
@end example
@end itemize
@node Sorting Text, Numbering Text, Underlining Text, Formatting Text
@comment node-name, next, previous, up
@section Sorting Text
@cindex sorting text
@cindex text, sorting
@pindex sort
@noindent
You can sort a list in a text file with @code{sort}. By default, it
outputs text in ascending alphabetical order; use the @samp{-r} option
to reverse the sort and output text in descending alphabetical order.
For example, suppose a file @file{provinces} contains the following:
@example
Shantung
Honan
Szechwan
Hunan
Kiangsu
Kwangtung
Fukien
@end example
@itemize @bullet
@item
To sort the file @file{provinces} and output all lines in ascending
order, type:
@example
@cartouche
$ @kbd{sort provinces @key{RET}}
Fukien
Honan
Hunan
Kiangsu
Kwangtung
Shantung
Szechwan
$
@end cartouche
@end example
@item
To sort the file @file{provinces} and output all lines in descending
order, type:
@example
@cartouche
$ @kbd{sort -r provinces @key{RET}}
Szechwan
Shantung
Kwangtung
Kiangsu
Hunan
Honan
Fukien
$
@end cartouche
@end example
@end itemize
The following table describes some of @code{sort}'s options.
@multitable @columnfractions .2 .8
@item @sc{Option}
@tab @sc{Description}
@item @code{-b}
@tab Ignore leading blanks on each line when sorting.
@item @code{-d}
@tab Sort in ``phone directory'' order, with only letters, digits, and
blanks being sorted.
@item @code{-f}
@tab When sorting, @emph{fold} lowercase letters into their uppercase
equivalent, so that differences in case are ignored.
@item @code{-i}
@tab Ignore all spaces and all non-typewriter characters when sorting.
@item @code{-n}
@tab Sort numerically instead of by character value.
@item @code{-o @var{file}}
@tab Write output to @var{file} instead of standard output.
@end multitable
@node Numbering Text, Reversing Text, Sorting Text, Formatting Text
@comment node-name, next, previous, up
@section Numbering Lines of Text
@cindex numbering lines of text
@cindex text, numbering lines of
@pindex cat
@pindex nl
@pindex less
@noindent
There are several ways to number lines of text.
One way to do it is to use the @code{nl} (``number lines'') tool. Its
default action is to write its input (either the file names given as an
argument, or the standard input) to the standard output, with an
indentation and all non-empty lines preceded with line numbers.
@itemize @bullet
@item
To peruse the file @file{report} with each line of the file preceded by
line numbers, type:
@example
$ @kbd{nl report | less @key{RET}}
@end example
@end itemize
You can set the numbering style with the @samp{-b} option followed by an
argument. The following table lists the possible arguments and describes
the numbering style they select.
@multitable @columnfractions .30 .70
@item @sc{Argument}
@tab @sc{Numbering Style}
@item @code{a}
@tab Number all lines.
@item @code{t}
@tab Number only non-blank lines. This is the default.
@item @code{n}
@tab Do not number lines.
@item @code{p@var{regexp}}
@tab Only number lines that contain the regular expression @var{regexp}
(@pxref{Regexps, , Regular Expressions---Matching Text Patterns}).
@end multitable
The default is for line numbers to start with one, and increment by
one. Set the initial line number by giving an argument to the @samp{-v}
option, and set the increment by giving an argument to the @samp{-i}
option.
@itemize @bullet
@item
To output the file @file{report} with each line of the file preceded by
line numbers, starting with the number two and counting by fours, type:
@example
$ @kbd{nl -v 2 -i 4 report @key{RET}}
@end example
@item
To number only the lines of the file @file{cantos} that begin with a
period (@samp{.}), starting numbering at zero and using a numbering
increment of five, and to write the output to @file{cantos.numbered},
type:
@example
$ @kbd{nl -i 5 -v 0 -b p'^\.' cantos > cantos.numbered @key{RET}}
@end example
@end itemize
The other way to number lines is to use @code{cat} with one of the
following two options: the @samp{-n} option numbers each line of its
input text, while the @samp{-b} option only numbers non-blank lines.
@itemize @bullet
@item
To peruse the text file @file{report} with each line of the file
numbered, type:
@example
$ @kbd{cat -n report | less @key{RET}}
@end example
@item
To peruse the text file @file{report} with each non-blank line of the file
numbered, type:
@example
$ @kbd{cat -b report | less @key{RET}}
@end example
@end itemize
In the preceding examples, output from @code{cat} is piped to
@code{less} for perusal; the original file is not altered.
To take an input file, number its lines, and then write the
line-numbered version to a new file, send the standard output of the
@code{cat} command to the new file to write.
@itemize @bullet
@item
To write a line-numbered version of file @file{report} to file
@file{report.lines}, type:
@example
$ @kbd{cat -n report > report.lines @key{RET}}
@end example
@end itemize
@node Reversing Text, , Numbering Text, Formatting Text
@comment node-name, next, previous, up
@section Reversing Text
@cindex reversing text
@cindex text, reversing
@pindex tac
@pindex rev
@noindent
The @code{tac} command is similar to @code{cat}, but it outputs text in
reverse order. There is another difference---@code{tac} works on
@dfn{records}, sections of text with separator strings, instead of lines
of text. Its default separator string is the linebreak character, so by
default @code{tac} outputs files in line-for-line reverse order.
@itemize @bullet
@item
To output the file @file{prizes} in line-for-line reverse order,
type:
@example
$ @kbd{tac prizes @key{RET}}
@end example
@end itemize
Specify a different separator with the @samp{-s} option. This is often
useful when specifying non-printing characters such as formfeeds. To
specify such a character, use the ANSI-C method of quoting
(@pxref{Quoting, , Passing Special Characters to Commands}).
@itemize @bullet
@item
To output @file{prizes} in page-for-page reverse order, type:
@example
$ @kbd{tac -s $'\f' prizes @key{RET}}
@end example
@end itemize
The preceding example uses the formfeed, or page break, character as
the delimiter, and so it outputs the file @file{prizes} in page-for-page
reverse order, with the last page output first.
Use the @samp{-r} option to use a regular expression for the separator
string (@pxref{Regexps, , Regular Expressions---Matching Text
Patterns}). You can build regular expressions to output text in
word-for-word and character-for-character reverse order:
@itemize @bullet
@item
To output @file{prizes} in word-for-word reverse order, type:
@example
$ @kbd{tac -r -s '[^a-zA-z0-9\-]' prizes @key{RET}}
@end example
@item
To output @file{prizes} in character-for-character reverse order, type:
@example
$ @kbd{tac -r -s '.\| @key{RET}
' prizes @key{RET}}
@end example
@end itemize
To reverse the characters on each @emph{line}, use @code{rev}.
@itemize @bullet
@item
To output @file{prizes} with the characters on each line reversed, type:
@example
$ @kbd{rev prizes @key{RET}}
@end example
@end itemize
|