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

📄 textproc.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
CLASS="COMMAND">egrep</B>, <BCLASS="COMMAND">fgrep</B>.	      They are handy for searching through a mixed set of files,	      some compressed, some not.</P><P><ANAME="BZGREPREF"></A></P><P>To search <AHREF="filearchiv.html#BZIPREF">bzipped</A>	      files, use <BCLASS="COMMAND">bzgrep</B>.</P></TD></TR></TABLE></DIV></DD><DT><ANAME="LOOKREF"></A><BCLASS="COMMAND">look</B></DT><DD><P>The command <BCLASS="COMMAND">look</B> works like	      <BCLASS="COMMAND">grep</B>, but does a lookup on	      a <SPANCLASS="QUOTE">"dictionary,"</SPAN> a sorted word list.	      By default, <BCLASS="COMMAND">look</B> searches for a match	      in <TTCLASS="FILENAME">/usr/dict/words</TT>, but a different	      dictionary file may be specified.</P><DIVCLASS="EXAMPLE"><HR><ANAME="LOOKUP"></A><P><B>Example 15-19. Checking words in a list for validity</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# lookup: Does a dictionary lookup on each word in a data file.   3&nbsp;   4&nbsp;file=words.data  # Data file from which to read words to test.   5&nbsp;   6&nbsp;echo   7&nbsp;   8&nbsp;while [ "$word" != end ]  # Last word in data file.   9&nbsp;do               # ^^^  10&nbsp;  read word      # From data file, because of redirection at end of loop.  11&nbsp;  look $word &#62; /dev/null  # Don't want to display lines in dictionary file.  12&nbsp;  lookup=$?      # Exit status of 'look' command.  13&nbsp;  14&nbsp;  if [ "$lookup" -eq 0 ]  15&nbsp;  then  16&nbsp;    echo "\"$word\" is valid."  17&nbsp;  else  18&nbsp;    echo "\"$word\" is invalid."  19&nbsp;  fi    20&nbsp;  21&nbsp;done &#60;"$file"    # Redirects stdin to $file, so "reads" come from there.  22&nbsp;  23&nbsp;echo  24&nbsp;  25&nbsp;exit 0  26&nbsp;  27&nbsp;# ----------------------------------------------------------------  28&nbsp;# Code below line will not execute because of "exit" command above.  29&nbsp;  30&nbsp;  31&nbsp;# Stephane Chazelas proposes the following, more concise alternative:  32&nbsp;  33&nbsp;while read word &#38;&#38; [[ $word != end ]]  34&nbsp;do if look "$word" &#62; /dev/null  35&nbsp;   then echo "\"$word\" is valid."  36&nbsp;   else echo "\"$word\" is invalid."  37&nbsp;   fi  38&nbsp;done &#60;"$file"  39&nbsp;  40&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><BCLASS="COMMAND">sed</B>, <BCLASS="COMMAND">awk</B></DT><DD><P>Scripting languages especially suited for parsing text	      files and command output. May be embedded singly or in	      combination in pipes and shell scripts.</P></DD><DT><BCLASS="COMMAND"><AHREF="sedawk.html#SEDREF">sed</A></B></DT><DD><P>Non-interactive <SPANCLASS="QUOTE">"stream editor"</SPAN>, permits using	      many <BCLASS="COMMAND">ex</B> commands in <AHREF="timedate.html#BATCHPROCREF">batch</A> mode. It finds many	      uses in shell scripts.</P></DD><DT><BCLASS="COMMAND"><AHREF="awk.html#AWKREF">awk</A></B></DT><DD><P>Programmable file extractor and formatter, good for	      manipulating and/or extracting fields (columns) in	      structured text files. Its syntax is similar to C.</P></DD><DT><ANAME="WCREF"></A><BCLASS="COMMAND">wc</B></DT><DD><P><ICLASS="FIRSTTERM">wc</I> gives a <SPANCLASS="QUOTE">"word	      count"</SPAN> on a file or I/O stream:	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash $ </TT><TTCLASS="USERINPUT"><B>wc /usr/share/doc/sed-4.1.2/README</B></TT> <TTCLASS="COMPUTEROUTPUT">13  70  447 README</TT> [13 lines  70 words  447 characters]</PRE></TD></TR></TABLE></P><P><TTCLASS="USERINPUT"><B>wc -w</B></TT> gives only the word count.</P><P><TTCLASS="USERINPUT"><B>wc -l</B></TT> gives only the line count.</P><P><TTCLASS="USERINPUT"><B>wc -c</B></TT> gives only the byte count.</P><P><TTCLASS="USERINPUT"><B>wc -m</B></TT> gives only the character count.</P><P><TTCLASS="USERINPUT"><B>wc -L</B></TT> gives only the length of the longest line.</P><P>Using <BCLASS="COMMAND">wc</B> to count how many	    <TTCLASS="FILENAME">.txt</TT> files are in current working directory:	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;$ ls *.txt | wc -l   2&nbsp;#  Will work as long as none of the "*.txt" files   3&nbsp;#+ have a linefeed embedded in their name.   4&nbsp;   5&nbsp;#  Alternative ways of doing this are:   6&nbsp;#      find . -maxdepth 1 -name \*.txt -print0 | grep -cz .   7&nbsp;#      (shopt -s nullglob; set -- *.txt; echo $#)   8&nbsp;   9&nbsp;#  Thanks, S.C.</PRE></TD></TR></TABLE>	    </P><P>Using <BCLASS="COMMAND">wc</B> to total up the size of all the	      files whose names begin with letters in the range d - h	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>wc [d-h]* | grep total | awk '{print $3}'</B></TT> <TTCLASS="COMPUTEROUTPUT">71832</TT> 	      </PRE></TD></TR></TABLE>	    </P><P>Using <BCLASS="COMMAND">wc</B> to count the instances of the	      word <SPANCLASS="QUOTE">"Linux"</SPAN> in the main source file for	      this book.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>grep Linux abs-book.sgml | wc -l</B></TT> <TTCLASS="COMPUTEROUTPUT">50</TT> 	      </PRE></TD></TR></TABLE>	    </P><P>See also <AHREF="filearchiv.html#EX52">Example 15-38</A> and <AHREF="redircb.html#REDIR4">Example 19-8</A>.</P><P>Certain commands include some of the	      functionality of <BCLASS="COMMAND">wc</B> as options.	      	    <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;... | grep foo | wc -l   2&nbsp;# This frequently used construct can be more concisely rendered.   3&nbsp;   4&nbsp;... | grep -c foo   5&nbsp;# Just use the "-c" (or "--count") option of grep.   6&nbsp;   7&nbsp;# Thanks, S.C.</PRE></TD></TR></TABLE></P></DD><DT><ANAME="TRREF"></A><BCLASS="COMMAND">tr</B></DT><DD><P>character translation filter.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><AHREF="special-chars.html#UCREF">Must use quoting and/or	      brackets</A>, as appropriate. Quotes prevent the	      shell from reinterpreting the special characters in	      <BCLASS="COMMAND">tr</B> command sequences. Brackets should be	      quoted to prevent expansion by the shell.  </P></TD></TR></TABLE></DIV><P>Either <TTCLASS="USERINPUT"><B>tr "A-Z" "*" &#60;filename</B></TT>	      or <TTCLASS="USERINPUT"><B>tr A-Z \* &#60;filename</B></TT> changes	      all the uppercase letters in <TTCLASS="FILENAME">filename</TT>	      to asterisks (writes to <TTCLASS="FILENAME">stdout</TT>).	      On some systems this may not work, but <TTCLASS="USERINPUT"><B>tr A-Z	      '[**]'</B></TT> will.</P><P><ANAME="TROPTIONS"></A></P><P>The <TTCLASS="OPTION">-d</TT> option deletes a range of	      characters.	    <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;echo "abcdef"                 # abcdef   2&nbsp;echo "abcdef" | tr -d b-d     # aef   3&nbsp;   4&nbsp;   5&nbsp;tr -d 0-9 &#60;filename   6&nbsp;# Deletes all digits from the file "filename".</PRE></TD></TR></TABLE></P><P>The <TTCLASS="OPTION">--squeeze-repeats</TT> (or              <TTCLASS="OPTION">-s</TT>) option deletes all but the              first instance of a string of consecutive characters.              This option is useful for removing excess <AHREF="special-chars.html#WHITESPACEREF">whitespace</A>.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo "XXXXX" | tr --squeeze-repeats 'X'</B></TT> <TTCLASS="COMPUTEROUTPUT">X</TT></PRE></TD></TR></TABLE></P><P>The <TTCLASS="OPTION">-c</TT> <SPANCLASS="QUOTE">"complement"</SPAN>	      option <ICLASS="FIRSTTERM">inverts</I> the character set to	      match. With this option, <BCLASS="COMMAND">tr</B> acts only	      upon those characters <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN> matching	      the specified set.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo "acfdeb123" | tr -c b-d +</B></TT> <TTCLASS="COMPUTEROUTPUT">+c+d+b++++</TT></PRE></TD></TR></TABLE>            </P><P>Note that <BCLASS="COMMAND">tr</B> recognizes <AHREF="regexp.html#POSIXREF">POSIX character classes</A>.	         <ANAME="AEN10535"HREF="#FTN.AEN10535">[1]</A>	      </P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo "abcd2ef1" | tr '[:alpha:]' -</B></TT> <TTCLASS="COMPUTEROUTPUT">----2--1</TT> 	      </PRE></TD></TR></TABLE>	    </P><DIVCLASS="EXAMPLE"><HR><ANAME="EX49"></A><P><B>Example 15-20. <ICLASS="FIRSTTERM">toupper</I>: Transforms a file	      to all uppercase.</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Changes a file to all uppercase.   3&nbsp;   4&nbsp;E_BADARGS=65   5&nbsp;   6&nbsp;if [ -z "$1" ]  # Standard check for command line arg.   7&nbsp;then   8&nbsp;  echo "Usage: `basename $0` filename"   9&nbsp;  exit $E_BADARGS  10&nbsp;fi    11&nbsp;  12&nbsp;tr a-z A-Z &#60;"$1"  13&nbsp;  14&nbsp;# Same effect as above, but using POSIX character set notation:  15&nbsp;#        tr '[:lower:]' '[:upper:]' &#60;"$1"  16&nbsp;# Thanks, S.C.  17&nbsp;  18&nbsp;exit 0  19&nbsp;  20&nbsp;#  Exercise:  21&nbsp;#  Rewrite this script to give the option of changing a file  22&nbsp;#+ to *either* upper or lowercase.</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="LOWERCASE"></A><P><B>Example 15-21. <ICLASS="FIRSTTERM">lowercase</I>: Changes all	      filenames in working directory to lowercase.</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;#   3&nbsp;#  Changes every filename in working directory to all lowercase.   4&nbsp;#   5&nbsp;#  Inspired by a script of John Dubois,   6&nbsp;#+ which was translated into Bash by Chet Ramey,   7&nbsp;#+ and considerably simplified by the author of the ABS Guide.   8&nbsp;   9&nbsp;  10&nbsp;for filename in *                # Traverse all files in directory.  11&nbsp;do  12&nbsp;   fname=`basename $filename`  13&nbsp;   n=`echo $fname | tr A-Z a-z`  # Change name to lowercase.  14&nbsp;   if [ "$fname" != "$n" ]       # Rename only files not already lowercase.  15&nbsp;   then  16&nbsp;     mv $fname $n  17&nbsp;   fi    18&nbsp;done     19&nbsp;  20&nbsp;exit $?  21&nbsp;  22&nbsp;  23&nbsp;# Code below this line will not execute because of "exit".  24&nbsp;#--------------------------------------------------------#  25&nbsp;# To run it, delete script above line.  26&nbsp;  27&nbsp;# The above script will not work on filenames containing blanks or newlines.  28&nbsp;# Stephane Chazelas therefore suggests the following alternative:  29&nbsp;

⌨️ 快捷键说明

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