Given its heritage in Unix, it's no surprise that GNU has a wealth of text processing tools and techniques to aid coders, writers, and publishers in editing and otherwise manipulating text documents.
This three-part series will introduce the bulk of those tools and utilities (plus some techniques) and include links to learning more. This part (part 2) will cover the formatting and printing utilities avialable on GNU. The previous sections -- part 1A & 1B -- introduced the basic commands.
To learn the basic functions of any of the commands below, type the name of the command followed by "--help". For example, to learn more about the "less" command, you would type (at the command prompt):
$ less --help
(Don't type the dollar sign "$", that simply represents the user command prompt.)
Also, if you need more detailed information about any of the following commands you can read their "man" or "info" pages by typing the word "man" or "info" respectively, followed by the command name. To see either manual for the "less" command type:
$ man less
$ info less
tr is the transform or transliterate command. It is used to change one character set to another. It is most often used to convert case. For example, if you wanted to transform a string of characters from all lowercase to all uppercase you would type:
$ echo "all uppercase" | tr a-z A-Z
tr can be used to change between many other character sets. It can also be used to delete, squeeze, or truncate character strings.
To learn more about tr:
- The tr Command
- Understanding Linux / UNIX tr command
- tr Man Page
- Translating a range of characters using the tr utility
- tr - translate or delete specific characters
- 8 Linux TR Command Examples
nl is the GNU line numbering utility. It's basic usage is very simple:
$ nl name-of-text-file
This will print the file to stdout (by default your screen) and will place the number of each line at the beginning of every line.
nl can use a number of different numbering styles and formats.
To learn more about nl:
fold is the GNU command to force lines to wrap at a certain length. The default line length is 80 characters. To force lines to wrap at 18 characters do:
$ echo This is a test, this is only a test. | fold -w 18
Notice that the lines wrap exactly at 18 characters regardless of where that point is. If it's in the middle of a word, it splits to the word over two lines. To prevent this and force the line to wrap at the first space before the character limit, add the "-s" option to the end of the command:
$ echo This is a test, this is only a test. | fold -w 18 -s
fold also can use bytes instead of characters, with the "-b" option.
To learn more about fold:
fmt is GNU's simple formatting utility. It performs many functions such as word wrapping and spacing and it is capable of operating only on designated strings. In addition to its standard mode, it can function in crown margin and split-only modes. A basic example of fmt is:
$ fmt -w 50 name-of-text-file
This performs much like the "fold" command above, wrapping the text at 50 characters.
To learn more about fmt:
To format text for printing use the pr utility. This command will paginate and/or columnate text documents for printing. Pagination separates sections of the text with a number of blank lines for printing to paper. For example:
$ pr -l 15 -w 65 name-of-text-file
This will format the given text file into pages that are 15 lines long and 65 characters in width. By default it will create top and bottom margins that are five lines each and it will print a header, in the top margin, that includes the date, the file's name, and the page number.
This is just the tip of the iceberg with pr, this little utility has numerous other options.
To learn more about pr:
Printing with lpr & lp
GNU uses the Common UNIX Printing System (CUPS) currently maintained by Apple. Because of the historical manner in which Unixoid systems developed there are two different printing commands contained in the CUPS suite of tools: "lpr" which was developed for Berkley systems and "lp" used with System V Unix systems. Although they are functionally equivalent, "lp" does support a slightly more sophisticated set of options. Choosing one over the other is largely a matter of taste.
Basic usage is:
$ lpr name-of-text-file
$ lp name-of-text-file
Both of these commands will send the named file to the default printer. There are many more options, and both commands can take stdin and they can be used in pipelines, as well.
To learn more about printing with GNU:
- The Printing HOWTO
- Command-Line Printing and Options
- lpr Man Page
- lpr Command-Line Options
- How to Send a File to a Remote Printer Using lpr & rpr
- lp Man Page
- CUPS Documentation
- CUPS Home Page
Copyright © 2013 Russell James
Permission is granted to copy, distribute and/or modify this document under the terms of the Creative Commons Attribution-ShareAlike 3.0 Unported license.