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

📄 ch34_01.htm

📁 the unix power tools
💻 HTM
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 34] The sed Stream Editor</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:47:08Z"><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="part05.htm"TITLE="V. Text Editing "><LINKREL="prev"HREF="ch33_12.htm"TITLE="33.12 Versions of awk "><LINKREL="next"HREF="ch34_02.htm"TITLE="34.2 Invoking sed "></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_12.htm"TITLE="33.12 Versions of awk "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 33.12 Versions of awk "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 34</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch34_02.htm"TITLE="34.2 Invoking sed "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.2 Invoking sed "BORDER="0"></A></TD></TR></TABLE>&nbsp;<HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="CHAPTER"><H1CLASS="chapter"><ACLASS="title"NAME="UPT-CHP-34">34. The sed Stream Editor</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#UPT-ART-7996"TITLE="34.1 Two Things You Must Know About sed ">Two Things You Must Know About sed </A><BR><ACLASS="sect1"HREF="ch34_02.htm"TITLE="34.2 Invoking sed ">Invoking sed </A><BR><ACLASS="sect1"HREF="ch34_03.htm"TITLE="34.3 Testing and Using a sed Script: checksed, runsed ">Testing and Using a sed Script: checksed, runsed </A><BR><ACLASS="sect1"HREF="ch34_04.htm"TITLE="34.4 sed Addressing Basics ">sed Addressing Basics </A><BR><ACLASS="sect1"HREF="ch34_05.htm"TITLE="34.5 Order of Commands in a Script ">Order of Commands in a Script </A><BR><ACLASS="sect1"HREF="ch34_06.htm"TITLE="34.6 One Thing at a Time ">One Thing at a Time </A><BR><ACLASS="sect1"HREF="ch34_07.htm"TITLE="34.7 Delimiting a Regular Expression ">Delimiting a Regular Expression </A><BR><ACLASS="sect1"HREF="ch34_08.htm"TITLE="34.8 Newlines in a sed Replacement ">Newlines in a sed Replacement </A><BR><ACLASS="sect1"HREF="ch34_09.htm"TITLE="34.9 Referencing the Search String in a Replacement ">Referencing the Search String in a Replacement </A><BR><ACLASS="sect1"HREF="ch34_10.htm"TITLE="34.10 Referencing Portions of a Search String ">Referencing Portions of a Search String </A><BR><ACLASS="sect1"HREF="ch34_11.htm"TITLE="34.11 Search &amp; Replacement: One Match Among Many ">Search &amp; Replacement: One Match Among Many </A><BR><ACLASS="sect1"HREF="ch34_12.htm"TITLE="34.12 Transformations on Text ">Transformations on Text </A><BR><ACLASS="sect1"HREF="ch34_13.htm"TITLE="34.13 Hold Space: The Set-Aside Buffer ">Hold Space: The Set-Aside Buffer </A><BR><ACLASS="sect1"HREF="ch34_14.htm"TITLE="34.14 Transforming Part of a Line ">Transforming Part of a Line </A><BR><ACLASS="sect1"HREF="ch34_15.htm"TITLE="34.15 Making Edits Across Line Boundaries ">Making Edits Across Line Boundaries </A><BR><ACLASS="sect1"HREF="ch34_16.htm"TITLE="34.16 The Deliberate Scrivener ">The Deliberate Scrivener </A><BR><ACLASS="sect1"HREF="ch34_17.htm"TITLE="34.17 Searching for Patterns Split Across Lines ">Searching for Patterns Split Across Lines </A><BR><ACLASS="sect1"HREF="ch34_18.htm"TITLE="34.18 Multiline Delete ">Multiline Delete </A><BR><ACLASS="sect1"HREF="ch34_19.htm"TITLE="34.19 Making Edits Everywhere Except... ">Making Edits Everywhere Except... </A><BR><ACLASS="sect1"HREF="ch34_20.htm"TITLE="34.20 The sed Test Command ">The sed Test Command </A><BR><ACLASS="sect1"HREF="ch34_21.htm"TITLE="34.21 Uses of the sed Quit Command ">Uses of the sed Quit Command </A><BR><ACLASS="sect1"HREF="ch34_22.htm"TITLE="34.22 Dangers of the sed Quit Command ">Dangers of the sed Quit Command </A><BR><ACLASS="sect1"HREF="ch34_23.htm"TITLE="34.23 sed Newlines, Quoting, and Backslashes in a Shell Script">sed Newlines, Quoting, and Backslashes in a Shell Script</A><BR><ACLASS="sect1"HREF="ch34_24.htm"TITLE="34.24 Quick Reference: sed ">Quick Reference: sed </A></P><P></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-7996">34.1 Two Things You Must Know About sed </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-38243"></A><ACLASS="indexterm"NAME="UPT-ART-7996-IX-SED-EDITOR"></A>If you are already familiar with global edits in othereditors like <EMCLASS="emphasis">vi</EM> or <EMCLASS="emphasis">ex</EM>, you know most of what you need toknow to begin to use <EMCLASS="emphasis">sed</EM>. There are two things, though, thatmake it very different:</P><OLCLASS="orderedlist"><LICLASS="listitem"><PCLASS="para">It doesn't change the file it edits.It is just what its name says:a &quot;<EMCLASS="emphasis">s</EM>tream <EMCLASS="emphasis">ed</EM>;;itor&quot;-designed to take a stream of data from<SPANCLASS="link">standard input (<ACLASS="linkend"HREF="ch13_01.htm#UPT-ART-1023"TITLE="Using Standard Input and Output">13.1</A>)</SPAN>or a file, transform it, and pass it to<SPANCLASS="link">standard output (<ACLASS="linkend"HREF="ch13_01.htm#UPT-ART-1023"TITLE="Using Standard Input and Output">13.1</A>)</SPAN>.If you want to edit a file, you have to write a <SPANCLASS="link">shell wrapper (<ACLASS="linkend"HREF="ch34_03.htm"TITLE="Testing and Using a sed Script: checksed, runsed ">34.3</A>)</SPAN>to capture standard output and write it back into your original file.</P></LI><LICLASS="listitem"><PCLASS="para"><EMCLASS="emphasis">sed</EM> commands are implicitly global. In an editor like<EMCLASS="emphasis">ex</EM>, the command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">s/old/new/</PRE></BLOCKQUOTE></P><PCLASS="para">will change &quot;old&quot; to &quot;new&quot; only on the current line unless you usethe global command or various addressing symbols to apply it toadditional lines. In <EMCLASS="emphasis">sed</EM>, exactly the opposite is true.A command like the one above will be applied to all lines in a file.Addressing symbols are used to <EMCLASS="emphasis">limit</EM> the extent of the match.(However, like <EMCLASS="emphasis">ex</EM>, only the first occurrence of a pattern ona given line will be changed unless the <EMCLASS="emphasis">g</EM> flag is added to theend of the substitution command.)</P></LI></OL><PCLASS="para">If all you want to do is make simple substitutions, you're ready togo. If you want to do more than that, <EMCLASS="emphasis">sed</EM> has some unique andpowerful commands.</P><PCLASS="para">This chapter makes no attempt to cover everything there is to knowabout <EMCLASS="emphasis">sed</EM>. Article<ACLASS="xref"HREF="ch34_24.htm"TITLE="Quick Reference: sed ">34.24</A>contains a complete quick reference to <EMCLASS="emphasis">sed</EM> commands, with manyexamples, because we use so many <EMCLASS="emphasis">sed</EM> scripts elsewhere in thisbook, and we need a &quot;dictionary&quot; so beginners can interpret them.But for the most part, this chapter contains advice on working with<EMCLASS="emphasis">sed</EM> and extended explanations of how to use some of its moredifficult commands.<ACLASS="indexterm"NAME="AUTOID-38278"></A></P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">TOR</SPAN></P></DIV></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_12.htm"TITLE="33.12 Versions of awk "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 33.12 Versions of awk "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_02.htm"TITLE="34.2 Invoking sed "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.2 Invoking sed "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">33.12 Versions of awk </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.2 Invoking sed </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 + -