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

📄 textproc.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 5 页
字号:
>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	    <ICLASS="EMPHASIS">.txt</I> 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 have a linefeed in their name.   3&nbsp;   4&nbsp;# Alternative ways of doing this are:   5&nbsp;#      find . -maxdepth 1 -name \*.txt -print0 | grep -cz .   6&nbsp;#      (shopt -s nullglob; set -- *.txt; echo $#)   7&nbsp;   8&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 12-35</A> and <AHREF="redircb.html#REDIR4">Example 16-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>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="EMPHASIS">inverts</I> the character set to	      match. With this option, <BCLASS="COMMAND">tr</B> acts only	      upon those characters <ICLASS="EMPHASIS">not</I> 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="AEN8254"HREF="#FTN.AEN8254">[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 12-18. <BCLASS="COMMAND">toupper</B>: 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 12-19. <BCLASS="COMMAND">lowercase</B>: 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;  30&nbsp;  31&nbsp;for filename in *    # Not necessary to use basename,  32&nbsp;                     # since "*" won't return any file containing "/".  33&nbsp;do n=`echo "$filename/" | tr '[:upper:]' '[:lower:]'`  34&nbsp;#                             POSIX char set notation.  35&nbsp;#                    Slash added so that trailing newlines are not  36&nbsp;#                    removed by command substitution.  37&nbsp;   # Variable substitution:  38&nbsp;   n=${n%/}          # Removes trailing slash, added above, from filename.  39&nbsp;   [[ $filename == $n ]] || mv "$filename" "$n"  40&nbsp;                     # Checks if filename already lowercase.  41&nbsp;done  42&nbsp;  43&nbsp;exit $?</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="DU"></A><P><B>Example 12-20. <BCLASS="COMMAND">Du</B>: DOS to UNIX text file conversion.</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Du.sh: DOS to UNIX text file converter.   3&nbsp;   4&nbsp;E_WRONGARGS=65   5&nbsp;   6&nbsp;if [ -z "$1" ]   7&nbsp;then   8&nbsp;  echo "Usage: `basename $0` filename-to-convert"   9&nbsp;  exit $E_WRONGARGS  10&nbsp;fi  11&nbsp;  12&nbsp;NEWFILENAME=$1.unx  13&nbsp;  14&nbsp;CR='\015'  # Carriage return.  15&nbsp;           # 015 is octal ASCII code for CR.  16&nbsp;           # Lines in a DOS text file end in CR-LF.  17&nbsp;           # Lines in a UNIX text file end in LF only.  18&nbsp;  19&nbsp;tr -d $CR &#60; $1 &#62; $NEWFILENAME  20&nbsp;# Delete CR's and write to new file.  21&nbsp;  22&nbsp;echo "Original DOS text file is \"$1\"."  23&nbsp;echo "Converted UNIX text file is \"$NEWFILENAME\"."  24&nbsp;  25&nbsp;exit 0  26&nbsp;  27&nbsp;# Exercise:  28&nbsp;# --------  29&nbsp;# Change the above script to convert from UNIX to DOS.</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="ROT13"></A><P><B>Example 12-21. <BCLASS="COMMAND">rot13</B>: rot13, ultra-weak encryption.</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# rot13.sh: Classic rot13 algorithm,   3&nbsp;#           encryption that might fool a 3-year old.   4&nbsp;   5&nbsp;# Usage: ./rot13.sh filename   6&nbsp;# or     ./rot13.sh &#60;filename   7&nbsp;# or     ./rot13.sh and supply keyboard input (stdin)   8&nbsp;   9&nbsp;cat "$@" | tr 'a-zA-Z' 'n-za-mN-ZA-M'   # "a" goes to "n", "b" to "o", etc.  10&nbsp;#  The 'cat "$@"' construction  11&nbsp;#+ permits getting input either from stdin or from files.  12&nbsp;  13&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="CRYPTOQUOTE"></A><P><B>Example 12-22. Generating <SPANCLASS="QUOTE">"Crypto-Quote"</SPAN> Puzzles</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# crypto-quote.sh: Encrypt quotes   3&nbsp;   4&nbsp;#  Will encrypt famous quotes in a simple monoalphabetic substitution.   5&nbsp;#  The result is similar to the "Crypto Quote" puzzles   6&nbsp;#+ seen in the Op Ed pages of the Sunday paper.   7&nbsp;   8&nbsp;   9&nbsp;key=ETAOINSHRDLUBCFGJMQPVWZYXK  10&nbsp;# The "key" is nothing more than a scrambled alphabet.  11&nbsp;# Changing the "key" changes the encryption.  12&nbsp;  13&nbsp;# The 'cat "$@"' construction gets input either from stdin or from files.  14&nbsp;# If using stdin, terminate input with a Control-D.  15&nbsp;# Otherwise, specify filename as command-line parameter.  16&nbsp;  17&nbsp;cat "$@" | tr "a-z" "A-Z" | tr "A-Z" "$key"  18&nbsp;#        |  to uppercase  |     encrypt         19&nbsp;# Will work on lowercase, uppercase, or mixed-case quotes.  20&nbsp;# Passes non-alphabetic characters through unchanged.  21&nbsp;  22&nbsp;  23&nbsp;# Try this script with something like:  24&nbsp;# "Nothing so needs reforming as other people's habits."  25&nbsp;# --Mark Twain  26&nbsp;#  27&nbsp;# Output is:  28&nbsp;# "CFPHRCS QF CIIOQ MINFMBRCS EQ FPHIM GIFGUI'Q HETRPQ."  29&nbsp;# --BEML PZERC  30&nbsp;  31&nbsp;# To reverse the encryption:  32&nbsp;# cat "$@" | tr "$key" "A-Z"  33&nbsp;  34&nbsp;  35&nbsp;#  This simple-minded cipher can be broken by an average 12-year old  36&nbsp;#+ using only pencil and paper.  37&nbsp;  38&nbsp;exit 0  39&nbsp;  40&nbsp;#  Exercise:  41&nbsp;#  --------  42&nbsp;#  Modify the script so that it will either encrypt or decrypt,  43&nbsp;#+ depending on command-line argument(s).</PRE></TD></TR></TABLE><HR></DIV><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN8282"></A><P><B><BCLASS="COMMAND">tr</B> variants</B></P><P>	    The <BCLASS="COMMAND">tr</B> utility has two historic	    variants. The BSD version does not use brackets	    (<TTCLASS="USERINPUT"><B>tr a-z A-Z</B></TT>), but the SysV one does	    (<TTCLASS="USERINPUT"><B>tr '[a-z]' '[A-Z]'</B></TT

⌨️ 快捷键说明

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