📄 textproc.html
字号:
>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 $ ls *.txt | wc -l 2 # Will work as long as none of the "*.txt" files have a linefeed in their name. 3 4 # Alternative ways of doing this are: 5 # find . -maxdepth 1 -name \*.txt -print0 | grep -cz . 6 # (shopt -s nullglob; set -- *.txt; echo $#) 7 8 # 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 ... | grep foo | wc -l 2 # This frequently used construct can be more concisely rendered. 3 4 ... | grep -c foo 5 # Just use the "-c" (or "--count") option of grep. 6 7 # 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" "*" <filename</B></TT> or <TTCLASS="USERINPUT"><B>tr A-Z \* <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 echo "abcdef" # abcdef 2 echo "abcdef" | tr -d b-d # aef 3 4 5 tr -d 0-9 <filename 6 # 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 #!/bin/bash 2 # Changes a file to all uppercase. 3 4 E_BADARGS=65 5 6 if [ -z "$1" ] # Standard check for command line arg. 7 then 8 echo "Usage: `basename $0` filename" 9 exit $E_BADARGS 10 fi 11 12 tr a-z A-Z <"$1" 13 14 # Same effect as above, but using POSIX character set notation: 15 # tr '[:lower:]' '[:upper:]' <"$1" 16 # Thanks, S.C. 17 18 exit 0 19 20 # Exercise: 21 # Rewrite this script to give the option of changing a file 22 #+ 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 #!/bin/bash 2 # 3 # Changes every filename in working directory to all lowercase. 4 # 5 # Inspired by a script of John Dubois, 6 #+ which was translated into Bash by Chet Ramey, 7 #+ and considerably simplified by the author of the ABS Guide. 8 9 10 for filename in * # Traverse all files in directory. 11 do 12 fname=`basename $filename` 13 n=`echo $fname | tr A-Z a-z` # Change name to lowercase. 14 if [ "$fname" != "$n" ] # Rename only files not already lowercase. 15 then 16 mv $fname $n 17 fi 18 done 19 20 exit $? 21 22 23 # Code below this line will not execute because of "exit". 24 #--------------------------------------------------------# 25 # To run it, delete script above line. 26 27 # The above script will not work on filenames containing blanks or newlines. 28 # Stephane Chazelas therefore suggests the following alternative: 29 30 31 for filename in * # Not necessary to use basename, 32 # since "*" won't return any file containing "/". 33 do n=`echo "$filename/" | tr '[:upper:]' '[:lower:]'` 34 # POSIX char set notation. 35 # Slash added so that trailing newlines are not 36 # removed by command substitution. 37 # Variable substitution: 38 n=${n%/} # Removes trailing slash, added above, from filename. 39 [[ $filename == $n ]] || mv "$filename" "$n" 40 # Checks if filename already lowercase. 41 done 42 43 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 #!/bin/bash 2 # Du.sh: DOS to UNIX text file converter. 3 4 E_WRONGARGS=65 5 6 if [ -z "$1" ] 7 then 8 echo "Usage: `basename $0` filename-to-convert" 9 exit $E_WRONGARGS 10 fi 11 12 NEWFILENAME=$1.unx 13 14 CR='\015' # Carriage return. 15 # 015 is octal ASCII code for CR. 16 # Lines in a DOS text file end in CR-LF. 17 # Lines in a UNIX text file end in LF only. 18 19 tr -d $CR < $1 > $NEWFILENAME 20 # Delete CR's and write to new file. 21 22 echo "Original DOS text file is \"$1\"." 23 echo "Converted UNIX text file is \"$NEWFILENAME\"." 24 25 exit 0 26 27 # Exercise: 28 # -------- 29 # 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 #!/bin/bash 2 # rot13.sh: Classic rot13 algorithm, 3 # encryption that might fool a 3-year old. 4 5 # Usage: ./rot13.sh filename 6 # or ./rot13.sh <filename 7 # or ./rot13.sh and supply keyboard input (stdin) 8 9 cat "$@" | tr 'a-zA-Z' 'n-za-mN-ZA-M' # "a" goes to "n", "b" to "o", etc. 10 # The 'cat "$@"' construction 11 #+ permits getting input either from stdin or from files. 12 13 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 #!/bin/bash 2 # crypto-quote.sh: Encrypt quotes 3 4 # Will encrypt famous quotes in a simple monoalphabetic substitution. 5 # The result is similar to the "Crypto Quote" puzzles 6 #+ seen in the Op Ed pages of the Sunday paper. 7 8 9 key=ETAOINSHRDLUBCFGJMQPVWZYXK 10 # The "key" is nothing more than a scrambled alphabet. 11 # Changing the "key" changes the encryption. 12 13 # The 'cat "$@"' construction gets input either from stdin or from files. 14 # If using stdin, terminate input with a Control-D. 15 # Otherwise, specify filename as command-line parameter. 16 17 cat "$@" | tr "a-z" "A-Z" | tr "A-Z" "$key" 18 # | to uppercase | encrypt 19 # Will work on lowercase, uppercase, or mixed-case quotes. 20 # Passes non-alphabetic characters through unchanged. 21 22 23 # Try this script with something like: 24 # "Nothing so needs reforming as other people's habits." 25 # --Mark Twain 26 # 27 # Output is: 28 # "CFPHRCS QF CIIOQ MINFMBRCS EQ FPHIM GIFGUI'Q HETRPQ." 29 # --BEML PZERC 30 31 # To reverse the encryption: 32 # cat "$@" | tr "$key" "A-Z" 33 34 35 # This simple-minded cipher can be broken by an average 12-year old 36 #+ using only pencil and paper. 37 38 exit 0 39 40 # Exercise: 41 # -------- 42 # Modify the script so that it will either encrypt or decrypt, 43 #+ 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 + -