⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch33_02.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 33] 33.2 Writing Editing Scripts </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly &amp; Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:46:46Z"><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="ch33_01.htm"TITLE="33. Batch Editing"><LINKREL="prev"HREF="ch33_01.htm"TITLE="33.1 Why Line Editors Aren't Dinosaurs "><LINKREL="next"HREF="ch33_03.htm"TITLE="33.3 Line Addressing "></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="ch33_01.htm"TITLE="33.1 Why Line Editors Aren't Dinosaurs "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 33.1 Why Line Editors Aren't Dinosaurs "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 33<BR>Batch Editing</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch33_03.htm"TITLE="33.3 Line Addressing "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 33.3 Line Addressing "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7975">33.2 Writing Editing Scripts </A></H2><PCLASS="para">When you write a script thatcontains a series of editing actions and then run the scripton an input file, you take whatwould be a <EMCLASS="emphasis">hands-on</EM> procedure in an editor such as <EMCLASS="emphasis">vi</EM>and transform it into a <EMCLASS="emphasis">look-no-hands</EM> procedure.</P><PCLASS="para">When performing edits manually, you get to trust the cause-and-effectrelationship of entering an editing command and seeing the immediateresult.There is usually an &quot;undo&quot; command that allows you to reversethe effect of a command and return the text file to its previous state.Once you learn an interactive text editor, you havethe feeling of making changes in a safe and controlled manner, onestep at a time.</P><PCLASS="para">Most people new to &quot;power editing&quot; will feel there is greaterrisk in writing a script to perform a series of edits thanin making those changes manually.The fear is that by automating the task,something will happen that cannot be reversed.The object of learning scripting with <EMCLASS="emphasis">ex</EM> or <EMCLASS="emphasis">sed</EM> is to understand the commands well enough tosee that your results are predictable.In other words, you come to understand thecause-and-effect relationship between your editingscript and the output that you get.</P><PCLASS="para">This requires using the editor in a controlled, methodical way.Gradually, you will developmethods for creating and testing editing scripts. You will cometo rely upon these methods and gain confidencethat you know what your script is doing and why.</P><PCLASS="para">Here are a few tips:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">Carefully examine your input file, using <EMCLASS="emphasis">grep</EM>,before designing your script.</P></LI><LICLASS="listitem"><PCLASS="para">Start with a small sample of occurrences in a test file.Run your script on the sample and make sure the script is working.Remember, it's just as important to make sure the script <EMCLASS="emphasis">doesn't</EM>work where you <EMCLASS="emphasis">don't</EM> want it to. Then increase the size of the sample.Try to increase the complexity of the input.</P></LI><LICLASS="listitem"><PCLASS="para">Work carefully, testing each command that you add to a script.Compare the output against the input file to see what haschanged. Prove to yourself that your script is complete.Your script may work perfectly, based on your assumptionsof what is in the input file, but your assumptionsmay be wrong.</P></LI><LICLASS="listitem"><PCLASS="para"><EMCLASS="emphasis">Be pragmatic!</EM>Try to accomplish what you can with your scriptbut understand that it doesn't have to do 100&nbsp;percent of the job.If you encounter difficult situations, check andsee how frequently they occur.Sometimes it's better to do a few remaining edits manually.</P></LI></OL><PCLASS="para">If you can add to these tips with your experience,tack them on.</P><PCLASS="para">[Okay, I will: use a<SPANCLASS="link">revision control system (<ACLASS="linkend"HREF="ch20_12.htm"TITLE="Protecting Files with SCCS or RCS ">20.12</A>)</SPAN>to keep previous versions.That makes it easy to undo your edits. -<EMCLASS="emphasis">JP</EM>&nbsp;]</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD</SPAN> <SPANCLASS="bibliomisc">from O'Reilly &amp; Associates' <CITECLASS="citetitle">sed &amp; awk</CITE></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="ch33_01.htm"TITLE="33.1 Why Line Editors Aren't Dinosaurs "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 33.1 Why Line Editors Aren't Dinosaurs "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="ch33_03.htm"TITLE="33.3 Line Addressing "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 33.3 Line Addressing "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">33.1 Why Line Editors Aren't Dinosaurs </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">33.3 Line Addressing </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 &amp; 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 + -