📄 0221-0223.html
字号:
<HTML>
<HEAD>
<TITLE>Sams Teach Yourself Linux in 24 Hours:Text Processing:EarthWeb Inc.-</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311623 //-->
<!-- TITLE=Sams Teach Yourself Linux in 24 Hours//-->
<!-- AUTHOR=Bill Ball//-->
<!-- PUBLISHER=Macmillan Computer Publishing//-->
<!-- IMPRINT=Sams//-->
<!-- CHAPTER=14 //-->
<!-- PAGES=0211-0228 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0217-0220.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0224-0226.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-221"><P>Page 221</P></A>
<P>Figure 14.5.<BR>
The jed editor has <br>
built-in help and text <br>
menus you can use as <br>
a shortcut to keyboard <br>
commands.</P>
<P><a href="javascript:displayWindow('images/ch14fg05.jpg', 288, 216)"><img src="images/tn_ch14fg05.jpg"></a><BR>
</P>
<H4><A NAME="ch14_ 10">
Changing Text with sed and Other Filters
</A></H4>
<P>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.
</P>
<P>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.
</P>
<P>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.
</P>
<P>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.
</P>
<P>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</P>
<A NAME="PAGENUM-222"><P>Page 222</P></A>
<P>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.
</P>
<!-- CODE //-->
<PRE>
# 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.
...
</PRE>
<!-- END CODE //-->
<P>You can change this document to all uppercase with the following:
</P>
<!-- CODE //-->
<PRE>
# 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.
...
</PRE>
<!-- END CODE //-->
<P>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:
</P>
<!-- CODE //-->
<PRE>
# 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.
...
</PRE>
<!-- END CODE //-->
<P>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:
</P>
<A NAME="PAGENUM-223"><P>Page 223</P></A>
<!-- CODE //-->
<PRE>
# 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
...
</PRE>
<!-- END CODE //-->
<P>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:
</P>
<!-- CODE //-->
<PRE>
# 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
...
</PRE>
<!-- END CODE //-->
<P>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:
</P>
<!-- CODE //-->
<PRE>
# 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
...
</PRE>
<!-- END CODE //-->
<P>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</P>
<P><CENTER>
<a href="0217-0220.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0224-0226.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -