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

📄 sedawk.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>A Sed and Awk Micro-Primer</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="PREVIOUS"TITLE="Reference Cards"HREF="refcards.html"><LINKREL="NEXT"TITLE="Awk"HREF="awk.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="APPENDIX"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="refcards.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="awk.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="APPENDIX"><H1><ANAME="SEDAWK"></A>Appendix C. A Sed and Awk Micro-Primer</H1><P><ANAME="SEDREF"></A></P><P>This is a very brief introduction to the <BCLASS="COMMAND">sed</B>	and <BCLASS="COMMAND">awk</B> text processing utilities. We will	deal with only a few basic commands here, but that will suffice	for understanding simple sed and awk constructs within shell	scripts.</P><P><BCLASS="COMMAND">sed</B>: a non-interactive text file editor</P><P><BCLASS="COMMAND">awk</B>: a field-oriented pattern processing         language with a C-style syntax</P><P>For all their differences, the two utilities share a similar	 invocation syntax, both use <AHREF="regexp.html#REGEXREF">regular	 expressions </A>, both read input by default	 from <TTCLASS="FILENAME">stdin</TT>, and both output to	 <TTCLASS="FILENAME">stdout</TT>. These are well-behaved UNIX tools,	 and they work together well. The output from one can be piped	 into the other, and their combined capabilities give shell	 scripts some of the power of Perl.</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>One important difference between the utilities is	 that while shell scripts can easily pass arguments to sed, it	 is more complicated for awk (see <AHREF="wrapper.html#COLTOTALER">Example 33-5</A>	 and <AHREF="ivr.html#COLTOTALER2">Example 9-25</A>).	 </P></TD></TR></TABLE></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="AEN21068"></A>C.1. Sed</H1><P>Sed is a non-interactive          <ANAME="AEN21071"HREF="#FTN.AEN21071">[1]</A>	line editor. It receives text input, whether from	<TTCLASS="FILENAME">stdin</TT> or from a file, performs certain	operations on specified lines of the input, one line at a time,	then outputs the result to <TTCLASS="FILENAME">stdout</TT> or to	a file.  Within a shell script, sed is usually one of several	tool components in a pipe.</P><P>Sed determines which lines of its input that it will	operate on from the <ICLASS="FIRSTTERM">address range</I> passed	to it.	  <ANAME="AEN21077"HREF="#FTN.AEN21077">[2]</A>	Specify this address range either by line number or by a	pattern to match. For example, <TTCLASS="REPLACEABLE"><I>3d</I></TT>	signals sed to delete line 3 of the input, and	<TTCLASS="REPLACEABLE"><I>/windows/d</I></TT> tells sed that	you want every line of the input containing a match to	<SPANCLASS="QUOTE">"windows"</SPAN> deleted.</P><P>Of all the operations in the sed toolkit, we will focus	primarily on the three most commonly used	ones. These are <BCLASS="COMMAND">p</B>rinting (to	<TTCLASS="FILENAME">stdout</TT>), <BCLASS="COMMAND">d</B>eletion,	and <BCLASS="COMMAND">s</B>ubstitution.</P><P><ANAME="SEDBASICTABLE"></A></P><DIVCLASS="TABLE"><HR><ANAME="AEN21090"></A><P><B>Table C-1. Basic sed operators</B></P><TABLEBORDER="1"CLASS="CALSTABLE"><THEAD><TR><THALIGN="LEFT"VALIGN="TOP">Operator</TH><THALIGN="LEFT"VALIGN="TOP">Name</TH><THALIGN="LEFT"VALIGN="TOP">Effect</TH></TR></THEAD><TBODY><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">[address-range]/p</TT></TD><TDALIGN="LEFT"VALIGN="TOP">print</TD><TDALIGN="LEFT"VALIGN="TOP">Print [specified address range]</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">[address-range]/d</TT></TD><TDALIGN="LEFT"VALIGN="TOP">delete</TD><TDALIGN="LEFT"VALIGN="TOP">Delete [specified address range]</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">s/pattern1/pattern2/</TT></TD><TDALIGN="LEFT"VALIGN="TOP">substitute</TD><TDALIGN="LEFT"VALIGN="TOP">Substitute pattern2 for first instance of pattern1 in a line</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">[address-range]/s/pattern1/pattern2/</TT></TD><TDALIGN="LEFT"VALIGN="TOP">substitute</TD><TDALIGN="LEFT"VALIGN="TOP">Substitute pattern2 for first instance of pattern1 in a	      line, over <TTCLASS="REPLACEABLE"><I>address-range</I></TT></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">[address-range]/y/pattern1/pattern2/</TT></TD><TDALIGN="LEFT"VALIGN="TOP">transform</TD><TDALIGN="LEFT"VALIGN="TOP">replace any character in pattern1 with the	        corresponding character in pattern2, over	      <TTCLASS="REPLACEABLE"><I>address-range</I></TT> (equivalent of	        <BCLASS="COMMAND">tr</B>)</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">g</TT></TD><TDALIGN="LEFT"VALIGN="TOP">global</TD><TDALIGN="LEFT"VALIGN="TOP">Operate on <SPANCLASS="emphasis"><ICLASS="EMPHASIS">every</I></SPAN> pattern match	        within each matched line of input</TD></TR></TBODY></TABLE><HR></DIV><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>Unless the <TTCLASS="OPTION">g</TT>	(<ICLASS="FIRSTTERM">global</I>) operator is appended to a	<ICLASS="FIRSTTERM">substitute</I> command, the substitution	operates only on the first instance of a pattern match within	each line.</P></TD></TR></TABLE></DIV><P>From the command line and in a shell script, a sed operation may       require quoting and certain options.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;sed -e '/^$/d' $filename   2&nbsp;# The -e option causes the next string to be interpreted as an editing instruction.   3&nbsp;#  (If passing only a single instruction to sed, the "-e" is optional.)   4&nbsp;#  The "strong" quotes ('') protect the RE characters in the instruction   5&nbsp;#+ from reinterpretation as special characters by the body of the script.   6&nbsp;# (This reserves RE expansion of the instruction for sed.)   7&nbsp;#   8&nbsp;# Operates on the text contained in file $filename.</PRE></TD></TR></TABLE></P><P>In certain cases, a <ICLASS="FIRSTTERM">sed</I> editing command will         not work with single quotes.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;filename=file1.txt   2&nbsp;pattern=BEGIN   3&nbsp;   4&nbsp;  sed "/^$pattern/d" "$filename"  # Works as specified.   5&nbsp;# sed '/^$pattern/d' "$filename"    has unexpected results.   6&nbsp;#        In this instance, with strong quoting (' ... '),   7&nbsp;#+      "$pattern" will not expand to "BEGIN".</PRE></TD></TR></TABLE></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><ICLASS="FIRSTTERM">Sed</I> uses the <TTCLASS="OPTION">-e</TT>	 option to specify that the following string is an instruction	 or set of instructions. If there is only a single instruction	 contained in the string, then this may be omitted.</P></TD></TR></TABLE></DIV><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;sed -n '/xzy/p' $filename   2&nbsp;# The -n option tells sed to print only those lines matching the pattern.   3&nbsp;# Otherwise all input lines would print.   4&nbsp;# The -e option not necessary here since there is only a single editing instruction.</PRE></TD></TR></TABLE></P><P><ANAME="SEDOPTABLE"></A></P><DIVCLASS="TABLE"><HR><ANAME="AEN21153"></A><P><B>Table C-2. Examples of sed operators</B></P><TABLEBORDER="1"CLASS="CALSTABLE"><THEAD><TR><THALIGN="LEFT"VALIGN="TOP">Notation</TH><THALIGN="LEFT"VALIGN="TOP">Effect</TH></TR></THEAD><TBODY><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">8d</TT></TD><TDALIGN="LEFT"VALIGN="TOP">Delete 8th line of input.</TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"><TTCLASS="OPTION">/^$/d</TT></TD><TD

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -