-->

Previous | Table of Contents | Next

Page 221

Figure 14.5.
The jed editor has
built-in help and text
menus you can use as
a shortcut to keyboard
commands.


Changing Text with sed and Other Filters

Until now, this hour has discussed interactive editors featuring cursor movement, menus, or other keyboard commands. There are several other programs, such as text filters, or stream editors, included with your system that can edit text.

If you've read the discussion in Hour 6, "Using the Shell," you'll recall that many Linux programs can use your shell's standard input and standard output. By using shell operators such as pipes or redirection operators, you can use Linux commands to filter text through pipelines to manipulate or change the text stream. There also are several other programs, such as ex and sed, that are specifically designed to edit filtered text, and are called stream editors.

Thanks to the FSF folks, the GNU text-utils package of two dozen text utilities is also installed on your system. This collection includes: cat, cksum, comm, csplit, cut, expand, fmt, fold, head, join, md5sum, nl, od, paste, pr, sort, split, sum, tac, tail, tr, unexpand, uniq, and wc.

Some of these formatting commands, such as fmt and pr, are discussed in the next hour, "Preparing Documents," but this section shows you how you can use several others to manipulate text. For example, the tr, or transliterate command, can be used to work on streams of text to translate, squeeze, or delete characters.

The tr command works by taking sets, or lists of characters on the command line, and using these sets to translate input text. If you have a document in upper- and lowercase, but would

Page 222

like to change the text to all uppercase, you can tell the tr command to do this by specifying the two sets of characters. A public domain software license (found in the /usr/doc/shadow-utils directory) is used in the following example.


# cat LICENSE

...



1.  You may make and give away verbatim copies of the source form of the

Standard Version of this Package without restriction, provided that you

duplicate all of the original copyright notices and associated

disclaimers.



2.  You may apply bug fixes, portability fixes and other modifications

derived from the Public Domain or from the Copyright Holder.  A Package

modified in such a way shall still be considered the Standard Version.



...

You can change this document to all uppercase with the following:


# cat LICENSE | tr a-z A-Z

...



1.  YOU MAY MAKE AND GIVE AWAY VERBATIM COPIES OF THE SOURCE FORM OF THE

STANDARD VERSION OF THIS PACKAGE WITHOUT RESTRICTION, PROVIDED THAT YOU

DUPLICATE ALL OF THE ORIGINAL COPYRIGHT NOTICES AND ASSOCIATED

DISCLAIMERS.



2.  YOU MAY APPLY BUG FIXES, PORTABILITY FIXES AND OTHER MODIFICATIONS

DERIVED FROM THE PUBLIC DOMAIN OR FROM THE COPYRIGHT HOLDER.  A PACKAGE

MODIFIED IN SUCH A WAY SHALL STILL BE CONSIDERED THE STANDARD VERSION.

...

The document has been piped, using the cat command, through the tr command, specifying that you'd like the set of lowercase letters, from a to z, to be translated to uppercase, or A to Z. The tr command can also be used to translate individual characters. For example, you'll notice that two spaces are used following each number and a period in the example text. You can replace multiple occurrences of characters with a single character by using the -s, or squeeze command-line option:


# cat COPYING | tr -s " "

...



1. You may make and give away verbatim copies of the source form of the

Standard Version of this Package without restriction, provided that you

duplicate all of the original copyright notices and associated

disclaimers.



...

As you can see, the two spaces have been replaced with a single space. The tr command also filters input to other commands, such as cut, to quickly generate custom reports. For example, if you don't need all the information from the ls command's -l, or long format listing, and only want certain columns of the output, you can get any column you want by filtering the listing through several pipes:

Page 223


# ls -l

...

-rw-r--r--   1 root     root         4244 May  5  1997 CENSORSHIP

-rw-r--r--   1 root     root         3315 Jun  2  1997 CHARSETS

-rw-r--r--   1 root     root        12794 Jun  2  1997 CODINGS

-rw-r--r--   1 root     root         4976 Jun  9  1993 COOKIES

-rw-r--r--   1 root     root        17989 Sep 19 22:01 COPYING

-rw-r--r--   1 root     root         5595 Jun 18  1995 DEBUG

-r--r--r--   1 root     root         5633 Sep 19 22:01 DISTRIB

-rw-r--r--   1 root     root       955298 Nov  7 10:33 DOC-20.2.1

-rw-r--r--   1 root     root       131199 May 17  1997 FAQ

-r--r--r--   1 root     root        11494 Sep 19 22:01 FTP

...

This is a lot of information, but if you only want the permissions, size, and name of each file, you can quickly generate a custom listing with the following:


# ls -l | tr -s " " | cut -d" " -f1,8,9

...

-rw-r--r-- 4244 CENSORSHIP

-rw-r--r-- 3315 CHARSETS

-rw-r--r-- 12794 CODINGS

-rw-r--r-- 4976 COOKIES

-rw-r--r-- 17989 COPYING

-rw-r--r-- 5595 DEBUG

-r--r--r-- 5633 DISTRIB

-rw-r--r-- 955298 DOC-20.2.1

-rw-r--r-- 131199 FAQ

-r--r--r-- 11494 FTP

...

The listing now only shows the first, eighth, and ninth columns of the original listing, because the tr command squeezed multiple spaces into a single space. The output is then piped through the cut command, specifying a field delimiter, using the -d option (in this case, a space). Notice that although the first and second columns look okay, the third is a little ragged. If this still isn't what you want, clean it up by again using the tr command to replace the space delimiter with a tab:


# ls -l | tr -s " " | cut -d" " -f1,5,9 | tr " " `\t'

...

-rw-r--r--      4244    CENSORSHIP

-rw-r--r--      3315    CHARSETS

-rw-r--r--      12794   CODINGS

-rw-r--r--      4976    COOKIES

-rw-r--r--      17989   COPYING

-rw-r--r--      5595    DEBUG

-r--r--r--      5633    DISTRIB

-rw-r--r--      955298  DOC-20.2.1

-rw-r--r--      131199  FAQ

-r--r--r--      11494   FTP

...

You can use these filters to change text in your documents, but stream editors, such as the sed command, offer more capable approaches to editing text from the command line. For

Previous | Table of Contents | Next