📄 ch01_06.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 1] 1.6 Power Tools for Editing </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:31:00Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch01_01.htm"TITLE="1. Introduction"><LINKREL="prev"HREF="ch01_05.htm"TITLE="1.5 Anyone Can Program the Shell "><LINKREL="next"HREF="ch01_07.htm"TITLE="1.7 Power Grows on You "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch01_05.htm"TITLE="1.5 Anyone Can Program the Shell "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.5 Anyone Can Program the Shell "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 1<BR>Introduction</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch01_07.htm"TITLE="1.7 Power Grows on You "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.7 Power Grows on You "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1039">1.6 Power Tools for Editing </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-980"></A><ACLASS="indexterm"NAME="AUTOID-983"></A><ACLASS="indexterm"NAME="AUTOID-986"></A><ACLASS="indexterm"NAME="AUTOID-989"></A>My wife won't let me buy a power saw. She is afraid of anaccident if I use one.So I rely on a hand saw for a variety of weekend projects likebuilding shelves.However, if I made my living as a carpenter, I wouldhave to use a powersaw. The speed and efficiency provided by power toolswould be essential to being productive.</P><PCLASS="para">For people who create and modify text files,<SPANCLASS="link"><EMCLASS="emphasis">sed</EM> (<ACLASS="linkend"HREF="ch34_24.htm"TITLE="Quick Reference: sed ">34.24</A>)</SPAN>and<SPANCLASS="link"><EMCLASS="emphasis">awk</EM> (<ACLASS="linkend"HREF="ch33_11.htm"TITLE="Quick Reference: awk ">33.11</A>)</SPAN>are power tools for editing.Most of the things that you can do with these programscan be done interactively with a text editor. However,using <EMCLASS="emphasis">sed</EM> and <EMCLASS="emphasis">awk</EM> can save many hours ofrepetitive work in achieving the same result.</P><PCLASS="para"><EMCLASS="emphasis">sed</EM> and <EMCLASS="emphasis">awk</EM> are peculiar and it takes timeto learn them, butthe capabilities they providecan repay the learning many times over, especially if text editingis a normal part of your trade.</P><PCLASS="para">Both of these programs let you write editing scripts todo many of the things that you mightotherwise do laboriously with repeated commands in an editor like<SPANCLASS="link"><EMCLASS="emphasis">vi</EM> (<ACLASS="linkend"HREF="ch30_02.htm"TITLE="What We Cover ">30.2</A>)</SPAN>.</P><PCLASS="para">Even more important, they let you do<SPANCLASS="link"> edits on data that is streaming through UNIX pipes - (<ACLASS="linkend"HREF="ch43_21.htm"TITLE="Preprocessing troff Input with sed ">43.21</A>, <ACLASS="linkend"HREF="ch27_15.htm"TITLE="Narrowing a Search Quickly ">27.15</A>, <ACLASS="linkend"HREF="ch18_09.htm"TITLE="Renaming, Copying, or Comparing a Set of Files ">18.9</A>, <ACLASS="linkend"HREF="ch17_17.htm"TITLE="Duplicating a Directory Tree (No Pathnames with find {} Operator)">17.17</A>)</SPAN>data that may never bewritten back into a file.</P><PCLASS="para">However, the primary motivation for learning <EMCLASS="emphasis">sed</EM> and <EMCLASS="emphasis">awk</EM>is that they are useful for devising general solutions to textediting problems.For some people, myself included, the satisfaction of solving aproblem is the difference between work and drudgery. Giventhe choice of using <EMCLASS="emphasis">vi</EM> or <EMCLASS="emphasis">sed</EM>to make a series of repeated edits over a number of files,I will choose <EMCLASS="emphasis">sed</EM>, simply because it makes the problem moreinteresting to me. I am refining a solutioninstead of repeating a series of keystrokes.Besides, once I accomplish my task,I congratulate myself on being clever.I feel like I have done a littlebit of magic and spared myself some dull labor.</P><PCLASS="para">Initially, using <EMCLASS="emphasis">sed</EM> and <EMCLASS="emphasis">awk</EM> will seem like the long wayto accomplish a task. After several attempts you may concludethat the task would have been easier to do manually.Be patient. You not only have to learn how to use <EMCLASS="emphasis">sed</EM> and <EMCLASS="emphasis">awk</EM> butyou also need to learn to recognize situations where using thempays off. As you become more proficient, you will not only solve problemsmore quickly, you will solve a broader range of problems.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD</SPAN> <SPANCLASS="bibliomisc">from O'Reilly & Associates' <CITECLASS="citetitle">sed & awk</CITE>, Chapter 1</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch01_05.htm"TITLE="1.5 Anyone Can Program the Shell "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 1.5 Anyone Can Program the Shell "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch01_07.htm"TITLE="1.7 Power Grows on You "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 1.7 Power Grows on You "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">1.5 Anyone Can Program the Shell </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">1.7 Power Grows on You </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -