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

📄 ch45_30.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 2 页
字号:
></A><ACLASS="indexterm"NAME="AUTOID-54744"></A><ACLASS="indexterm"NAME="AUTOID-54747"></A>The Bourne shell<SPANCLASS="link"><EMCLASS="emphasis">set</EM> (<ACLASS="linkend"HREF="ch44_19.htm"TITLE="The Bourne Shell set Command ">44.19</A>)</SPAN>command can be used to parse a single-line string andstore it in the<SPANCLASS="link">command-line parameters (<ACLASS="linkend"HREF="ch44_15.htm"TITLE="Handling Command-Line Arguments in Shell Scripts ">44.15</A>)</SPAN><CODECLASS="literal">&quot;$@&quot;</CODE>, <CODECLASS="literal">$*</CODE>, <CODECLASS="literal">$1</CODE>, <CODECLASS="literal">$2</CODE>, and so on.Then you can also loop through the words with a<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>and use everything else the shell has for dealing with command-lineparameters.Also, you can set the<SPANCLASS="link"><EMCLASS="emphasis">IFS</EM> variable (<ACLASS="linkend"HREF="ch35_21.htm"TITLE="Using IFS to Split Strings ">35.21</A>)</SPAN>to control how the shell splitsthe string.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-232-SECT-1.4">45.30.4 Using sed </A></H3><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-54765"></A><ACLASS="indexterm"NAME="AUTOID-54768"></A><ACLASS="indexterm"NAME="AUTOID-54771"></A>The UNIX<SPANCLASS="link"><EMCLASS="emphasis">sed</EM> (<ACLASS="linkend"HREF="ch34_24.htm"TITLE="Quick Reference: sed ">34.24</A>)</SPAN>utility is good at parsing input that you may or may not be able tosplit into words otherwise, at finding a single line of text in a group andoutputting it, and many other things.In this example, I want to get the percentage-used of the filesystemmounted on <EMCLASS="emphasis">/home</EM>.That information is buried in the output of the<SPANCLASS="link"><EMCLASS="emphasis">df</EM> (<ACLASS="linkend"HREF="ch24_09.htm"TITLE="How Much Disk Space? ">24.9</A>)</SPAN>command.On my system, <EMCLASS="emphasis">df</EM> output looks like:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>df</B></CODE>Filesystem            kbytes    used   avail capacity  Mounted on   .../dev/sd3c            1294854  914230  251139    78%    /work/dev/sd4c             597759  534123    3861    99%    /home   ...</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-54784"></A>I want the number <EMCLASS="emphasis">99</EM> from the line ending with <EMCLASS="emphasis">/home</EM>.The <EMCLASS="emphasis">sed</EM> address <CODECLASS="literal">/&nbsp;\/home$/</CODE> will find that line (including aspace before the <EMCLASS="emphasis">/home</EM> makes sure the address doesn't match aline ending with <EMCLASS="emphasis">/something/home</EM>).The <EMCLASS="emphasis">-n</EM> option keeps <EMCLASS="emphasis">sed</EM> from printing any lines exceptthe line we ask it to print (with its <EMCLASS="emphasis">p</EM> command).I know that the &quot;capacity&quot; is the only word on the line that endswith a percent sign (<CODECLASS="literal">%</CODE>).A space after the first <CODECLASS="literal">.*</CODE> makes sure that <CODECLASS="literal">.*</CODE> doesn't&quot;eat&quot; the first digit of the number that we want to match by <CODECLASS="literal">[0-9]</CODE>.The <EMCLASS="emphasis">sed</EM><SPANCLASS="link">escaped-parenthesis operators (<ACLASS="linkend"HREF="ch34_10.htm"TITLE="Referencing Portions of a Search String ">34.10</A>)</SPAN>grab that number.Here goes:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">usage=`df | sed -n '/ \/home$/s/.* \([0-9][0-9]*\)%.*/\1/p'`</PRE></BLOCKQUOTE></P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-54805"></A><ACLASS="indexterm"NAME="AUTOID-54808"></A><ACLASS="indexterm"NAME="AUTOID-54811"></A><ACLASS="indexterm"NAME="AUTOID-54814"></A>Combining <EMCLASS="emphasis">sed</EM> with<SPANCLASS="link"><EMCLASS="emphasis">eval</EM> (<ACLASS="linkend"HREF="ch08_10.htm"TITLE="eval: When You Need Another Chance ">8.10</A>)</SPAN>lets you set several shell variablesat once from parts of the same line.Here's a command line that sets two shell variables from the <EMCLASS="emphasis">df</EM>output:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">eval `df |sed -n '/ \/home$/s/^[^ ]*  *\([0-9]*\)  *\([0-9]*\).*/kb=\1 u=\2/p'`</PRE></BLOCKQUOTE></P><PCLASS="para">The left-hand side of that substitution command has a regular expressionthat uses <EMCLASS="emphasis">sed</EM>'s escaped parenthesis operators.They grab the &quot;kbytes&quot; and &quot;used&quot; columns from the <EMCLASS="emphasis">df</EM>output.The right-hand side outputs the two <EMCLASS="emphasis">df</EM> values with Bourne shellvariable-assignmentcommands to set the <EMCLASS="emphasis">kb</EM> and <EMCLASS="emphasis">u</EM> variables.After <EMCLASS="emphasis">sed</EM> finishes, the resulting command line looks like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">eval kb=597759 u=534123</PRE></BLOCKQUOTE></P><PCLASS="para">Now <CODECLASS="literal">$kb</CODE> will give you <EMCLASS="emphasis">597759</EM>and <CODECLASS="literal">$u</CODE> contains <EMCLASS="emphasis">534123</EM>.</P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</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="ch45_29.htm"TITLE="45.29 Testing Characters in a String with expr "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 45.29 Testing Characters in a String with expr "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="ch45_31.htm"TITLE="45.31 Nested Command Substitution "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 45.31 Nested Command Substitution "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">45.29 Testing Characters in a String with expr </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">45.31 Nested Command Substitution </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 + -