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

📄 textproc.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 5 页
字号:
WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;File: 1.data   2&nbsp;   3&nbsp;100 Shoes   4&nbsp;200 Laces   5&nbsp;300 Socks</PRE></TD></TR></TABLE></P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;File: 2.data   2&nbsp;   3&nbsp;100 $40.00   4&nbsp;200 $1.00   5&nbsp;300 $2.00</PRE></TD></TR></TABLE></P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>join 1.data 2.data</B></TT> <TTCLASS="COMPUTEROUTPUT">File: 1.data 2.data 100 Shoes $40.00 200 Laces $1.00 300 Socks $2.00</TT> 	      </PRE></TD></TR></TABLE>	    </P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The tagged field appears only once in the	      output.</P></TD></TR></TABLE></DIV></DD><DT><BCLASS="COMMAND">head</B></DT><DD><P>lists the beginning of a file to	      <TTCLASS="FILENAME">stdout</TT> (the default is	      <TTCLASS="LITERAL">10</TT> lines, but this can be changed). It	      has a number of interesting options.	    <DIVCLASS="EXAMPLE"><HR><ANAME="SCRIPTDETECTOR"></A><P><B>Example 12-12. Which files are scripts?</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# script-detector.sh: Detects scripts within a directory.   3&nbsp;   4&nbsp;TESTCHARS=2    # Test first 2 characters.   5&nbsp;SHABANG='#!'   # Scripts begin with a "sha-bang."   6&nbsp;   7&nbsp;for file in *  # Traverse all the files in current directory.   8&nbsp;do   9&nbsp;  if [[ `head -c$TESTCHARS "$file"` = "$SHABANG" ]]  10&nbsp;  #      head -c2                      #!  11&nbsp;  #  The '-c' option to "head" outputs a specified  12&nbsp;  #+ number of characters, rather than lines (the default).  13&nbsp;  then  14&nbsp;    echo "File \"$file\" is a script."  15&nbsp;  else  16&nbsp;    echo "File \"$file\" is *not* a script."  17&nbsp;  fi  18&nbsp;done  19&nbsp;    20&nbsp;exit 0  21&nbsp;  22&nbsp;#  Exercises:  23&nbsp;#  ---------  24&nbsp;#  1) Modify this script to take as an optional argument  25&nbsp;#+    the directory to scan for scripts  26&nbsp;#+    (rather than just the current working directory).  27&nbsp;#  28&nbsp;#  2) As it stands, this script gives "false positives" for  29&nbsp;#+    Perl, awk, and other scripting language scripts.  30&nbsp;#     Correct this.</PRE></TD></TR></TABLE><HR></DIV>	    	    <DIVCLASS="EXAMPLE"><HR><ANAME="RND"></A><P><B>Example 12-13. Generating 10-digit random numbers</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# rnd.sh: Outputs a 10-digit random number   3&nbsp;   4&nbsp;# Script by Stephane Chazelas.   5&nbsp;   6&nbsp;head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'   7&nbsp;   8&nbsp;   9&nbsp;# =================================================================== #  10&nbsp;  11&nbsp;# Analysis  12&nbsp;# --------  13&nbsp;  14&nbsp;# head:  15&nbsp;# -c4 option takes first 4 bytes.  16&nbsp;  17&nbsp;# od:  18&nbsp;# -N4 option limits output to 4 bytes.  19&nbsp;# -tu4 option selects unsigned decimal format for output.  20&nbsp;  21&nbsp;# sed:   22&nbsp;# -n option, in combination with "p" flag to the "s" command,  23&nbsp;# outputs only matched lines.  24&nbsp;  25&nbsp;  26&nbsp;  27&nbsp;# The author of this script explains the action of 'sed', as follows.  28&nbsp;  29&nbsp;# head -c4 /dev/urandom | od -N4 -tu4 | sed -ne '1s/.* //p'  30&nbsp;# ----------------------------------&#62; |  31&nbsp;  32&nbsp;# Assume output up to "sed" --------&#62; |  33&nbsp;# is 0000000 1198195154\n  34&nbsp;  35&nbsp;#  sed begins reading characters: 0000000 1198195154\n.  36&nbsp;#  Here it finds a newline character,  37&nbsp;#+ so it is ready to process the first line (0000000 1198195154).  38&nbsp;#  It looks at its &#60;range&#62;&#60;action&#62;s. The first and only one is  39&nbsp;  40&nbsp;#   range     action  41&nbsp;#   1         s/.* //p  42&nbsp;  43&nbsp;#  The line number is in the range, so it executes the action:  44&nbsp;#+ tries to substitute the longest string ending with a space in the line  45&nbsp;#  ("0000000 ") with nothing (//), and if it succeeds, prints the result  46&nbsp;#  ("p" is a flag to the "s" command here, this is different from the "p" command).  47&nbsp;  48&nbsp;#  sed is now ready to continue reading its input. (Note that before  49&nbsp;#+ continuing, if -n option had not been passed, sed would have printed  50&nbsp;#+ the line once again).  51&nbsp;  52&nbsp;# Now, sed reads the remainder of the characters, and finds the end of the file.  53&nbsp;# It is now ready to process its 2nd line (which is also numbered '$' as  54&nbsp;# it's the last one).  55&nbsp;# It sees it is not matched by any &#60;range&#62;, so its job is done.  56&nbsp;  57&nbsp;#  In few word this sed commmand means:  58&nbsp;#  "On the first line only, remove any character up to the right-most space,  59&nbsp;#+ then print it."  60&nbsp;  61&nbsp;# A better way to do this would have been:  62&nbsp;#           sed -e 's/.* //;q'  63&nbsp;  64&nbsp;# Here, two &#60;range&#62;&#60;action&#62;s (could have been written  65&nbsp;#           sed -e 's/.* //' -e q):  66&nbsp;  67&nbsp;#   range                    action  68&nbsp;#   nothing (matches line)   s/.* //  69&nbsp;#   nothing (matches line)   q (quit)  70&nbsp;  71&nbsp;#  Here, sed only reads its first line of input.  72&nbsp;#  It performs both actions, and prints the line (substituted) before quitting  73&nbsp;#+ (because of the "q" action) since the "-n" option is not passed.  74&nbsp;  75&nbsp;# =================================================================== #  76&nbsp;  77&nbsp;# An even simpler altenative to the above one-line script would be:  78&nbsp;#           head -c4 /dev/urandom| od -An -tu4  79&nbsp;  80&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV>	    	      See also <AHREF="filearchiv.html#EX52">Example 12-35</A>.</P></DD><DT><BCLASS="COMMAND">tail</B></DT><DD><P>lists the end of a file to <TTCLASS="FILENAME">stdout</TT>	      (the default is <TTCLASS="LITERAL">10</TT> lines).  Commonly used	      to keep track of changes to a system logfile, using the	      <TTCLASS="OPTION">-f</TT> option, which outputs lines appended	      to the file.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX12"></A><P><B>Example 12-14. Using <BCLASS="COMMAND">tail</B> to monitor the system log</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;filename=sys.log   4&nbsp;   5&nbsp;cat /dev/null &#62; $filename; echo "Creating / cleaning out file."   6&nbsp;#  Creates file if it does not already exist,   7&nbsp;#+ and truncates it to zero length if it does.   8&nbsp;#  : &#62; filename   and   &#62; filename also work.   9&nbsp;  10&nbsp;tail /var/log/messages &#62; $filename    11&nbsp;# /var/log/messages must have world read permission for this to work.  12&nbsp;  13&nbsp;echo "$filename contains tail end of system log."  14&nbsp;  15&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/tip.png"HSPACE="5"ALT="Tip"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>To list a specific line of a text file,	        <AHREF="special-chars.html#PIPEREF">pipe</A> the output of	        <BCLASS="COMMAND">head</B> to <BCLASS="COMMAND">tail -1</B>.		For example <TTCLASS="USERINPUT"><B>head -8 database.txt | tail		-1</B></TT> lists the 8th line of the file		<TTCLASS="FILENAME">database.txt</TT>.</P><P>To set a variable to a given block of a text file:	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;var=$(head -$m $filename | tail -$n)   2&nbsp;   3&nbsp;# filename = name of file   4&nbsp;# m = from beginning of file, number of lines to end of block   5&nbsp;# n = number of lines to set variable to (trim from end of block)</PRE></TD></TR></TABLE></P></TD></TR></TABLE></DIV><P>See also <AHREF="moreadv.html#EX41">Example 12-5</A>, <AHREF="filearchiv.html#EX52">Example 12-35</A> and		<AHREF="debugging.html#ONLINE">Example 29-6</A>.</P></DD><DT><ANAME="GREPREF"></A><BCLASS="COMMAND">grep</B></DT><DD><P>A multi-purpose file search tool that uses	      <AHREF="regexp.html#REGEXREF">Regular Expressions</A>.	      It was originally a command/filter in the	      venerable <BCLASS="COMMAND">ed</B> line editor:	      <TTCLASS="USERINPUT"><B>g/re/p</B></TT> -- <ICLASS="EMPHASIS">global -	      regular expression - print</I>.</P><P><P><BCLASS="COMMAND">grep</B>   <TTCLASS="REPLACEABLE"><I>pattern</I></TT>  [<TTCLASS="REPLACEABLE"><I>file</I></TT>...]</P>Search the target file(s) for	      occurrences of <TTCLASS="REPLACEABLE"><I>pattern</I></TT>, where	      <TTCLASS="REPLACEABLE"><I>pattern</I></TT> may be literal text	      or a Regular Expression.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>grep '[rst]ystem.$' osinfo.txt</B></TT> <TTCLASS="COMPUTEROUTPUT">The GPL governs the distribution of the Linux operating system.</TT> 	      </PRE></TD></TR></TABLE>	      </P><P>If no target file(s) specified, <BCLASS="COMMAND">grep</B>	      works as a filter on <TTCLASS="FILENAME">stdout</TT>, as in	      a <AHREF="special-chars.html#PIPEREF">pipe</A>.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ps ax | grep clock</B></TT> <TTCLASS="COMPUTEROUTPUT">765 tty1     S      0:00 xclock 901 pts/1    S      0:00 grep clock</TT> 	      </PRE></TD></TR></TABLE>	      </P><P>The <TTCLASS="OPTION">-i</TT> option causes a case-insensitive	      search.</P><P>The <TTCLASS="OPTION">-w</TT> option matches only whole	      words.</P><P>The <TTCLASS="OPTION">-l</TT> option lists only the files in which	      matches were found, but not the matching lines.</P><P>The <TTCLASS="OPTION">-r</TT> (recursive) option searches files in	      the current working directory and all subdirectories below	      it.</P><P>The <TTCLASS="OPTION">-n</TT> option lists the matching lines,	      together with line numbers.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>grep -n Linux osinfo.txt</B></TT> <TTCLASS="COMPUTEROUTPUT">2:This is a file containing information about Linux. 6:The GPL governs the distribution of the Linux operating system.</TT> 	      </PRE></TD></TR></TABLE>	      </P><P>The <TTCLASS="OPTION">-v</TT> (or <TTCLASS="OPTION">--invert-match</TT>)	      option <ICLASS="EMPHASIS">filters out</I> matches.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;grep pattern1 *.txt | grep -v pattern2   2&nbsp;   3&nbsp;# Matches all lines in "*.txt" files containing "pattern1",   4&nbsp;# but ***not*** "pattern2".	      </PRE></TD></TR></TABLE></P><P>The <TTCLASS="OPTION">-c</TT> (<TTCLASS="OPTION">--count</TT>)	      option gives a numerical count of matches, rather than	      actually listing the matches.	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;grep -c txt *.sgml   # (number of occurrences of "txt" in "*.sgml" files)   2&nbsp;   3&nbsp;   4&nbsp;#   grep -cz .   5&nbsp;#            ^ dot   6&nbsp;# means count (-c) zero-separated (-z) items matching "."   7&nbsp;# that is, non-empty ones (containing at least 1 character).   8&nbsp;#    9&nbsp;printf 'a b\nc  d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz .     # 3  10&nbsp;printf 'a b\nc  d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz '$'   # 5  11&nbsp;printf 'a b\nc  d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -cz '^'   # 5  12&nbsp;#  13&nbsp;printf 'a b\nc  d\n\n\n\n\n\000\n\000e\000\000\nf' | grep -c '$'    # 9  14&nbsp;# By default, newline chars (\n) separate items to match.   15&nbsp;  16&nbsp;# Note that the -z option is GNU "grep" specific.  17&nbsp;  18&nbsp;  19&nbsp;# Thanks, S.C.</PRE></TD></TR></TABLE>            </P><P>When invoked with more than one target file given,	      <BCLASS="COMMAND">grep</B> specifies which file contains	      matches.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>grep Linux osinfo.txt misc.txt</B></TT> <TTCLASS="COMPUTEROUTPUT">osinfo.txt:This is a file containing information about Linux. osinfo.txt:The GPL governs the distribution of the Linux operating system. misc.txt:The Linux operating system is steadily gaining in popularity.</TT> 	      </PRE></TD></TR></TABLE>	    </P><DIVCLASS="TIP"><TABLECLASS="TIP"WIDTH="90%"BORDER="0"

⌨️ 快捷键说明

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