Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Complete Idiot's Guide to Linux
(Publisher: Macmillan Computer Publishing)
Author(s): Manuel Ricart
ISBN: 078971826x
Publication Date: 12/22/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Getting the Needed Input: Input Redirection

You can redirect input the same way you are able to redirect output. When a program’s input stream is redirected from a file, the program uses the characters contained in the file just as if you had typed them on your keyboard.

Input redirection is best illustrated with an example. Pretend that it’s your job to prepare memos for circulation. Each time your boss tells you that a memo is ready to be processed, you carry out a complex series of tasks. First, you load the file /incoming/memo.txt into vi. Then, you remove the first ten lines of text which contain some control information, and you save the file. Finally, you copy the file to /outgoing/memo.txt, where your boss retrieves it and sends it to everyone else. Is there a simpler way to do this?

Realizing that you are using the same series of keystrokes over and over again, you decide to simplify your task with input redirection. Using what you learned about vi in Chapter 13, “Text Editing Under a Shell” you create a text file called prepare.txt which looks like this:

     10dd
     :w/outgoing/memo.txt
     :q!

After creating this file, you try preparing a memo with the following command to the shell:

     vi /incoming/memo.txt < prepare.txt

When you press Enter to execute the command, vi responds:

     Vim: Warning: Input is not from a terminal

This is to tell you that vi will be redirecting its standard input stream from the file you created. A few moments later, your shell prompt returns. After checking the file /outgoing/memo.txt, you confirm that the memo was processed correctly. Using input redirection, a complex task has been made simpler.

Regular Expressions

Although regular expressions are an advanced topic, they can be quite useful if you understand them. So what exactly is a regular expression? It’s a pattern definition, also called wild cards, that can be used to define what text should look like and allow the shell to find a match. You can use regular expressions on a shell prompt to select groups of files that match the pattern without having to explicitly type the name of each file.

Regular expressions, or REGEXs as they are also called, are used by a variety of programs—including your shell. Sometimes the symbols used to represent the patterns will change a little in meaning, but in general, the basic syntax is the same.

Here’s an example of something you might want to do with REGEXs: Suppose that you have a directory with four files—file1.txt, file2.txt, file3.txt, and info.txt—and you want to move them to a different directory. What would you do?

You could execute this command:

     mv file1.txt file2.txt file3.txt info.txt ../somedir

Although no doubt that command would do what you want, it is not something you want to type often, especially if you have 400 files instead of just four! A regular expression can be used to solve this sort of problem. Instead of typing a monstrous command, you could type:

     mv * ../somedir

That looks easier, doesn’t it?

The asterisk, or star, is a special character among a group called metacharacters. The star defines a pattern that matches everything. Let’s take a look at the metacharacters and how you can use them.

Match Anything: *

The star matches any sequence of characters. That’s why I can say mv * to move all the files. You can place the star between other letters so that it matches words that might contain other characters. See Table 14.1 for examples of using the star.

Table 14.1 Examples of Using the Star

Examples Description

*.txt Matches all files that end with .txt
file* Matches all files beginning with the word file
file*.txt Matches all files beginning with the word file and ending with .txt

Match Any Letter: ?

The star is powerful, no doubt about it. Sometimes, in fact, it’s too powerful. Because it can stand for any number of characters, it can potentially match things you don’t want to match. Luckily, there is a more restrained metacharacter: The ? will match one letter exactly, regardless of what the character at that position is. However, there must be a letter in the position you specify, or there’s no match. See Table 14.2 for examples.

Table 14.2 Examples of Using the Question Mark

Example Description

file?.txt Matches all files that begin with file, are followed by a single character, and end in .txt
? Matches all files that have a one-letter name
?? Matches all files that have a two-letter name

Match in a Range: []

Sometimes you want to match a variety of options. Let’s say you wanted to use metacharacters to find file1.txt and file2.txt but not file3.txt. In that case, you need to provide a set of options enclosed by brackets, like this:

     file[12].txt

This pattern will match file1.txt and file2.txt—nothing else.

If you want to find a continuous range of characters—say you wanted to match the numbers 0 through 5—you can specify them as [0-5] instead of [012345].


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.