Monday, March 22, 2010

wild card characters use & pattern matching on UNIX Shell

As the shell reads each line, it "handles" any special characters. This includes variable evaluation (variables start with a "$)," and filename expansion. Expansion of filenames occurs when the characters "*," "?," or "[" occur in a word. A question mark matches a single character. An asterisk matches any number of characters, including none. Square brackets are used to specify a range or particular combination of characters. Inside square brackets, a hyphen is used to specify a range or characters. Also, if the first character inside the square brackets is an exclamation point, the complement of the range is used. Let me give some examples:
+-------------------------------------------------------------------------------+
|        Table 1     |
|    Examples of filename expansion    |
+-------------------------------------------------------------------------------+
|Pattern Matches        |
|*  Every file in the current directory    |
|?  Files consisting of one character    |
|??  Files consisting of two characters    |
|??*  Files consisting of two or more characters   |
|[abcdefg] Files consisting of a single letter from a to g.  |
|[gfedcba] Same as above       |
|[a-g]  Same as above       |
|[a-cd-g] Same as above       |
|[a-zA-Z0-9] Files that consist of a single letter or number   |
|[!a-zA-Z0-9] Files that consist of a single character not a letter or number |
|[a-zA-Z]* Files that start with a letter     |
|?[a-zA-Z]* Files whose second character matches a letter.   |
|*[0-9]  Files that end with a number     |
|?[0-9]  Two character filename that end with a number   |
|*.[0-9] Files that end with a dot and a number    |
+-------------------------------------------------------------------------------+
As you can see, the dot is not a special character. Filenames may or may not have a dot. UNIX Programers use the dot to standardize on the type of source code of each file, but that is just a convention. There is another convention, which concerns the shell:

No comments:

Post a Comment