📄 ch34_03.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 34] 34.3 Testing and Using a sed Script: checksed, runsed </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:10Z"><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_02.htm"TITLE="34.2 Invoking sed "><LINKREL="next"HREF="ch34_04.htm"TITLE="34.4 sed Addressing Basics "></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_02.htm"TITLE="34.2 Invoking sed "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.2 Invoking sed "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_04.htm"TITLE="34.4 sed Addressing Basics "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.4 sed Addressing Basics "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-0083">34.3 Testing and Using a sed Script: checksed, runsed </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-38332"></A>All but the simplest <EMCLASS="emphasis">sed</EM> scripts are often invoked from a "shell wrapper," a<SPANCLASS="link">shell script (<ACLASS="linkend"HREF="ch44_01.htm#UPT-ART-0065"TITLE="Everyone Should Learn Some Shell Programming ">44.1</A>)</SPAN>that invokes <EMCLASS="emphasis">sed</EM> and also contains the editing commands that <EMCLASS="emphasis">sed</EM> executes.A shell wrapper is an easy way to turn what could be a complexcommand line into a single-word command.The fact that <EMCLASS="emphasis">sed</EM> is being used might be transparent tousers of the command.</P><PCLASS="para">Two shell scripts that you should immediately arm yourself withare described here.Both use a shell<SPANCLASS="link"><EMCLASS="emphasis">for</EM> loop (<ACLASS="linkend"HREF="ch44_16.htm"TITLE="Handling Command-Line Arguments with a for Loop ">44.16</A>)</SPAN>to apply thesame edits to any number of files.But the first just shows the changes,so you can make sure that your edits were made correctly.The second writes the edits back into the original file,making them permanent.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-83-SECT-1.1">34.3.1 checksed </A></H3><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="checksed">checksed</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-38345"></A><ACLASS="indexterm"NAME="AUTOID-38347"></A>The shell script <EMCLASS="emphasis">checksed</EM> automates the processof checking the edits that <EMCLASS="emphasis">sed</EM> makes.It expects to find the script file, <EMCLASS="emphasis">sedscr</EM>, in the current directoryand applies these instructions to the input files named onthe command line.The output is shown by a pager program; the default pager is <EMCLASS="emphasis">more</EM>.</TD></TR></TABLE><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch13_13.htm"TITLE='13.13 The "Filename" - '>-</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">#! /bin/shscript=sedscrfor filedo echo "********** < = $file > = sed output **********" sed -f $script "$file" | diff "$file" -done | ${PAGER-more}</PRE></TD></TR></TABLE></P><PCLASS="para">For example:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>cat sedscr</B></CODE>s/jerry@ora\.com/jpeek@jpeek.com/g$ <CODECLASS="userinput"><B>checksed home.html new.html</B></CODE>********** < = home.html > = sed output **********102c102< <a href="mailto:jerry@ora.com">Email it</A> or use this form:--> <a href="mailto:jpeek@jpeek.com">Email it</A> or use this form:124c124< Page created by: <a href="mailto:jerry@ora.com">jerry@ora.com</a>--> Page created by: <a href="mailto:jpeek@jpeek.com">jpeek@jpeek.com</a>********** < = new.html > = sed output **********22c22< <a href="mailto:jerry@ora.com">Send comments</A> to me!---> <a href="mailto:jpeek@jpeek.com">Send comments</A> to me!</PRE></BLOCKQUOTE></P><PCLASS="para">If you find that your script did not produce the resultsyou expected, perfect the editing script and run <EMCLASS="emphasis">checksed</EM> again.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-83-SECT-1.2">34.3.2 runsed </A></H3><TABLECLASS="para.programreference"BORDER="1"><TR><THVALIGN="TOP"><ACLASS="programreference"HREF="examples/index.htm"TITLE="runsed">runsed</A><BR></TH><TDVALIGN="TOP"><ACLASS="indexterm"NAME="AUTOID-38368"></A><ACLASS="indexterm"NAME="AUTOID-38370"></A>The shell script <EMCLASS="emphasis">runsed</EM> was developed to make changes to a file permanently.It applies your <EMCLASS="emphasis">sedscr</EM> to an input file,creates a temporary file, then copies that file over the original.<EMCLASS="emphasis">runsed</EM> has several safety checks:</TD></TR></TABLE><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para">It won't edit the <EMCLASS="emphasis">sed</EM> script file (if you accidentally include<EMCLASS="emphasis">sedscr</EM> on the command line),</P></LI><LICLASS="listitem"><PCLASS="para">It complains if you try to edit an empty file or something that isn'ta file (like a directory),</P></LI><LICLASS="listitem"><PCLASS="para">If the <EMCLASS="emphasis">sed</EM> script doesn't produce any output, <EMCLASS="emphasis">runsed</EM>aborts instead of emptying your original file.</P></LI></UL><PCLASS="para"><EMCLASS="emphasis">runsed</EM> only modifies a file if your <EMCLASS="emphasis">sedscr</EM> made edits.So, the file's<SPANCLASS="link">timestamp (<ACLASS="linkend"HREF="ch16_05.htm"TITLE="The Three UNIX File Times ">16.5</A>)</SPAN>won't change if the file's contents weren't changed.</P><PCLASS="para">Like <EMCLASS="emphasis">checksed</EM>, <EMCLASS="emphasis">runsed</EM> expects to find a <EMCLASS="emphasis">sed</EM> scriptnamed <EMCLASS="emphasis">sedscr</EM> in the directory where you want to make the edits.(Article<ACLASS="xref"HREF="ch04_03.htm"TITLE="Organizing Nonexecutable Scripts ">4.3</A>describes a way to keep many <EMCLASS="emphasis">sed</EM> scripts.)Supply the name or names of the files to edit on the command line. <SPANCLASS="link">Shell metacharacters (<ACLASS="linkend"HREF="ch15_02.htm"TITLE="Filename Wildcards in a Nutshell ">15.2</A>)</SPAN>can be used to specify a set of files:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>runsed *.html</B></CODE>runsed: editing home.html: runsed: done with home.htmlrunsed: editing new.html: runsed: done with new.htmlrunsed: all done</PRE></BLOCKQUOTE></P><PCLASS="para"><EMCLASS="emphasis">runsed</EM> does not protect you from imperfect editing scripts.You should use <EMCLASS="emphasis">checksed</EM> first to verify your changes beforeactually making them permanent with <EMCLASS="emphasis">runsed</EM>. (You could also modify <EMCLASS="emphasis">runsed</EM> to keep backup copies of theoriginal versions.)</P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">DD</SPAN>, <SPANCLASS="authorinitials">JP</SPAN>, <SPANCLASS="authorinitials">TOR</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_02.htm"TITLE="34.2 Invoking sed "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 34.2 Invoking sed "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_04.htm"TITLE="34.4 sed Addressing Basics "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 34.4 sed Addressing Basics "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">34.2 Invoking sed </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.4 sed Addressing Basics </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 + -