📄 ch34_14.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 34] 34.14 Transforming Part of a Line </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:47:22Z"><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="ch34_01.htm"TITLE="34. The sed Stream Editor"><LINKREL="prev"HREF="ch34_13.htm"TITLE="34.13 Hold Space: The Set-Aside Buffer "><LINKREL="next"HREF="ch34_15.htm"TITLE="34.15 Making Edits Across Line Boundaries "></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="ch34_13.htm"TITLE="34.13 Hold Space: The Set-Aside Buffer "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.13 Hold Space: The Set-Aside Buffer "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 34<BR>The sed Stream Editor</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch34_15.htm"TITLE="34.15 Making Edits Across Line Boundaries "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.15 Making Edits Across Line Boundaries "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-8002">34.14 Transforming Part of a Line </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-8002-IX-SED-EDITOR-TRANSFORM-COMMAND"></A>The <SPANCLASS="link">transform command, <EMCLASS="emphasis">y</EM> (<ACLASS="linkend"HREF="ch34_12.htm"TITLE="Transformations on Text ">34.12</A>)</SPAN>,acts on the entire contents of the pattern space.It is something of a chore to do a letter-by-lettertransformation of a portion of the line,but it is possible (thoughconvoluted) as the following example will demonstrate.[The real importance of this example is probably not the use of the<EMCLASS="emphasis">y</EM> command, but the use of the hold space to isolate andpreserve part of the line. -<EMCLASS="emphasis">TOR</EM> ]</P><PCLASS="para">While working on a programming guide, we found that the namesof statements were entered inconsistently.They needed tobe uppercase, but some were lowercase while others hadan initial capital letter.While the task was simple - to capitalizethe name of the statement - there were nearly a hundred statementsand it seemed a tedious project to write that many explicitsubstitutions of the form:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">s/find the Match statement/find the MATCH statement/g</PRE></BLOCKQUOTE></P><PCLASS="para"> The transform command could do the lowercase-to-uppercaseconversion but it applies the conversionto the entire line.The hold space makes this taskpossible because we use it to store a copy of the input line whilewe isolate and convertthe statement name in the pattern space.Look at the script first:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen"># capitalize statement names/the .* statement/{ h s/.*the \(.*\) statement.*/\1/ y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/ G s/\(.*\)\n\(.*the \).*\( statement.*\)/\2\1\3/}</PRE></BLOCKQUOTE></P><PCLASS="para"> The address limits the procedure to lines that match <CODECLASS="literal">the .* statement</CODE>.Let's look at what each command does:</P><DLCLASS="variablelist"><DTCLASS="term"><CODECLASS="literal">h</CODE></DT><DDCLASS="listitem"><PCLASS="para">The hold command copiesthe current input line into the hold space.Using the sample line <CODECLASS="literal">find the Match statement</CODE>, we'll showwhat the contents of the pattern space and hold spacecontain.After the <EMCLASS="emphasis">h</EM> command, both the pattern spaceand the hold space are identical.</P><TABLECLASS="informaltable"><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">Pattern space</EM>:</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">find the Match statement</CODE></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">Hold space</EM>:</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">find the Match statement</CODE></TD></TR></TBODY></TABLE><PCLASS="para"></P></DD><DTCLASS="term"><CODECLASS="literal">s/.*the \(.*\) statement.*/\1/</CODE></DT><DDCLASS="listitem"><PCLASS="para"> The substitute command extracts the name of the statement fromthe line and replaces the entire line with it.</P><TABLECLASS="informaltable"><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">Pattern space</EM>:</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">Match</CODE></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">Hold space</EM>:</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">find the Match statement</CODE></TD></TR></TBODY></TABLE><PCLASS="para"></P></DD><DTCLASS="term"><CODECLASS="literal">y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/</CODE> </DT><DDCLASS="listitem"><PCLASS="para"> The transform command changes each lowercase letterto an uppercase letter.</P><TABLECLASS="informaltable"><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">Pattern space</EM>:</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">MATCH</CODE></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">Hold space</EM>:</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">find the Match statement</CODE></TD></TR></TBODY></TABLE><PCLASS="para"></P></DD><DTCLASS="term"><CODECLASS="literal">G</CODE></DT><DDCLASS="listitem"><PCLASS="para">The Get command appends the line saved in the hold spaceto the pattern space.The embedded newline from the Get command is shown as <CODECLASS="literal">\n</CODE>.</P><TABLECLASS="informaltable"><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">Pattern space</EM>:</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">MATCH\nfind the Match statement</CODE></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">Hold space</EM>:</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">find the Match statement</CODE></TD></TR></TBODY></TABLE><PCLASS="para"></P></DD><DTCLASS="term"><CODECLASS="literal">s/\(.*\)\n\(.*the \).*\( statement.*\)/\2\1\3/</CODE></DT><DDCLASS="listitem"><PCLASS="para"> The substitute command matches three different parts of the patternspace: (1) all characters up to the embedded newline,(2) all characters followingthe embedded newline and up to and including <CODECLASS="literal">the</CODE> followed by aspace, and (3) all characters beginning with a space and followed by<CODECLASS="literal">statement</CODE> up to the end of the pattern space.The nameof the statement as it appeared in the original line is matchedbut not saved.The replacement section ofthis command recalls the saved portions and reassembles themin a different order, puttingthe capitalized name of the command in between <CODECLASS="literal">the</CODE> and <CODECLASS="literal">statement</CODE>.</P><TABLECLASS="informaltable"><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">Pattern space</EM>:</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">find the MATCH statement</CODE></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><EMCLASS="emphasis">Hold space</EM>:</TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><CODECLASS="literal">find the Match statement</CODE></TD></TR></TBODY></TABLE><PCLASS="para"></P></DD></DL><PCLASS="para">Let's look at a test run.Here's our sample file:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">find the Match statementConsult the Get statement.using the Read statement to retrieve data</PRE></BLOCKQUOTE></P><PCLASS="para"> Running the script on the sample file produces:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">find the MATCH statementConsult the GET statement.using the READ statement to retrieve data</PRE></BLOCKQUOTE></P><PCLASS="para">As you can see from this script, the hold space can be skillfullyused to isolate and manipulate portions of the input line.<ACLASS="indexterm"NAME="AUTOID-38952"></A><ACLASS="indexterm"NAME="AUTOID-38953"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD</SPAN> <SPANCLASS="bibliomisc">from O'Reilly & Associates' <CITECLASS="citetitle">sed & 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="ch34_13.htm"TITLE="34.13 Hold Space: The Set-Aside Buffer "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.13 Hold Space: The Set-Aside Buffer "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="ch34_15.htm"TITLE="34.15 Making Edits Across Line Boundaries "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.15 Making Edits Across Line Boundaries "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">34.13 Hold Space: The Set-Aside Buffer </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">34.15 Making Edits Across Line Boundaries </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 + -