📄 sedawk.html
字号:
ALIGN="LEFT"VALIGN="TOP">Delete all blank lines.</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">1,/^$/d</TT></TD><TDALIGN="LEFT"VALIGN="TOP">Delete from beginning of input up to, and including first blank line.</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">/Jones/p</TT></TD><TDALIGN="LEFT"VALIGN="TOP">Print only lines containing <SPANCLASS="QUOTE">"Jones"</SPAN> (with <SPANCLASS="TOKEN">-n</SPAN> option).</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">s/Windows/Linux/</TT></TD><TDALIGN="LEFT"VALIGN="TOP">Substitute <SPANCLASS="QUOTE">"Linux"</SPAN> for first instance of <SPANCLASS="QUOTE">"Windows"</SPAN> found in each input line.</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">s/BSOD/stability/g</TT></TD><TDALIGN="LEFT"VALIGN="TOP">Substitute <SPANCLASS="QUOTE">"stability"</SPAN> for every instance of <SPANCLASS="QUOTE">"BSOD"</SPAN> found in each input line.</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">s/ *$//</TT></TD><TDALIGN="LEFT"VALIGN="TOP">Delete all spaces at the end of every line.</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">s/00*/0/g</TT></TD><TDALIGN="LEFT"VALIGN="TOP">Compress all consecutive sequences of zeroes into a single zero.</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">/GUI/d</TT></TD><TDALIGN="LEFT"VALIGN="TOP">Delete all lines containing <SPANCLASS="QUOTE">"GUI"</SPAN>.</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">s/GUI//g</TT></TD><TDALIGN="LEFT"VALIGN="TOP">Delete all instances of <SPANCLASS="QUOTE">"GUI"</SPAN>, leaving the remainder of each line intact.</TD></TR></TBODY></TABLE><HR></DIV><P>Substituting a zero-length string for another is equivalent to deleting that string within a line of input. This leaves the remainder of the line intact. Applying <TTCLASS="USERINPUT"><B>s/GUI//</B></TT> to the line <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="USERINPUT"><B>The most important parts of any application are its GUI and sound effects</B></TT></PRE></TD></TR></TABLE> results in <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="COMPUTEROUTPUT">The most important parts of any application are its and sound effects</TT></PRE></TD></TR></TABLE></P><P>A backslash forces the <BCLASS="COMMAND">sed</B> replacement command to continue on to the next line. This has the effect of using the <ICLASS="FIRSTTERM">newline</I> at the end of the first line as the <ICLASS="FIRSTTERM">replacement string</I>. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 s/^ */\ 2 /g</PRE></TD></TR></TABLE> This substitution replaces line-beginning spaces with a newline. The net result is to replace paragraph indents with a blank line between paragraphs.</P><P>An address range followed by one or more operations may require open and closed curly brackets, with appropriate newlines. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 /[0-9A-Za-z]/,/^$/{ 2 /^$/d 3 }</PRE></TD></TR></TABLE> This deletes only the first of each set of consecutive blank lines. That might be useful for single-spacing a text file, but retaining the blank line(s) between paragraphs.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The usual delimiter that <ICLASS="FIRSTTERM">sed</I> uses is <SPANCLASS="TOKEN">/</SPAN>. However, <SPANCLASS="emphasis"><ICLASS="EMPHASIS">sed</I></SPAN> allows other delimiters, such as <SPANCLASS="TOKEN">%</SPAN>. This is useful when <SPANCLASS="TOKEN">/</SPAN> is part of a replacement string, as in a file pathname. See <AHREF="loops.html#FINDSTRING">Example 10-9</A> and <AHREF="filearchiv.html#STRIPC">Example 15-31</A>.</P></TD></TR></TABLE></DIV><P><ANAME="DOUBLESPACE"></A></P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>A quick way to double-space a text file is <TTCLASS="USERINPUT"><B>sed G filename</B></TT>.</P></TD></TR></TABLE></DIV><P>For illustrative examples of sed within shell scripts, see: <OLTYPE="1"><LI><P><AHREF="wrapper.html#EX3">Example 33-1</A></P></LI><LI><P><AHREF="wrapper.html#EX4">Example 33-2</A></P></LI><LI><P><AHREF="moreadv.html#EX57">Example 15-3</A></P></LI><LI><P><AHREF="contributed-scripts.html#RN">Example A-2</A></P></LI><LI><P><AHREF="textproc.html#GRP">Example 15-17</A></P></LI><LI><P><AHREF="textproc.html#COL">Example 15-26</A></P></LI><LI><P><AHREF="contributed-scripts.html#BEHEAD">Example A-12</A></P></LI><LI><P><AHREF="contributed-scripts.html#TREE">Example A-17</A></P></LI><LI><P><AHREF="contributed-scripts.html#TREE2">Example A-18</A></P></LI><LI><P><AHREF="filearchiv.html#STRIPC">Example 15-31</A></P></LI><LI><P><AHREF="loops.html#FINDSTRING">Example 10-9</A></P></LI><LI><P><AHREF="mathc.html#BASE">Example 15-46</A></P></LI><LI><P><AHREF="contributed-scripts.html#MAILFORMAT">Example A-1</A></P></LI><LI><P><AHREF="textproc.html#RND">Example 15-14</A></P></LI><LI><P><AHREF="textproc.html#WF">Example 15-12</A></P></LI><LI><P><AHREF="contributed-scripts.html#LIFESLOW">Example A-10</A></P></LI><LI><P><AHREF="here-docs.html#SELFDOCUMENT">Example 18-12</A></P></LI><LI><P><AHREF="textproc.html#DICTLOOKUP">Example 15-18</A></P></LI><LI><P><AHREF="contributed-scripts.html#WHX">Example A-31</A></P></LI><LI><P><AHREF="contributed-scripts.html#TOHTML">Example A-26</A></P></LI></OL> </P><P>For a more extensive treatment of <ICLASS="FIRSTTERM">sed</I>, check the appropriate references in the <AHREF="biblio.html"><I>Bibliography</I></A>.</P></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN21071"HREF="sedawk.html#AEN21071">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Sed executes without user intervention.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN21077"HREF="sedawk.html#AEN21077">[2]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>If no address range is specified, the default is <SPANCLASS="emphasis"><ICLASS="EMPHASIS">all</I></SPAN> lines.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="refcards.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="awk.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Reference Cards</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"> </TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Awk</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -