Sams Teach Yourself Shell (Vps web hosting) Programming in 24 Hours
Sams Teach Yourself Shell Programming in 24 Hours Contents Index Hour 15: Text Filters Previous Chapter Next Chapter Sections in this Chapter: The head and tail Commands Summary Using grep Questions Counting Words Previous Section Next Section Counting Words The tr Command Sorting Numbers The sort Command Using Character Classes with tr The uniq Command Counting words is an essential capability in shell scripts. There are many ways to do it, with the easiest being the wc command. Unfortunately, it displays only the number of characters, words, or lines. What about when you need to count the number of occurrences of word in a file? The wc command falls short. In this section, you will solve this problem using the following commands: l tr l sort l uniq The tr command ( tr for transliterate) changes all the characters in one set into characters in a second set. Sometimes it deletes sets of characters. The sort command sorts the lines in an input file. If you don’t specify an input file, it sorts the lines given on STDIN. The uniq command ( uniq for unique) prints all the unique lines in a file. If a line occurs multiple times, only one copy of the line is printed out. It can also list the number of times a particular line was duplicated. I will use the text of this chapter, ch15.doc, as the input file for this example. The tr Command First, you need to eliminate all the punctuation and delimiters in the input file because the word end. and the word end are the same. You accomplish this task using the tr command. Its basic syntax is tr ’set1′ ’set2′ Here tr takes all the characters in set1 and transliterates them to the characters in set2. Usually, the characters themselves are used, but the standard C language escape sequences also work. To accomplish my first task, I used the following command: $ tr ‘!?”:;[]{}(),.’ ‘ ‘ < /home/ranga/docs/ch15.doc
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.