Freigeben über


Using filters

Used in conjunction with the command redirection pipe character (|), a command filter is a command within a command that reads the command's input, transforms the input, and then writes the output. Filter commands help you sort, view, and select parts of a command output.

Filter commands divide, rearrange, or extract portions of the information that passes through them. The following table lists filter commands that are available in Windows XP.

Command

Description

more

Displays the contents of a file or the output of a command in one Command Prompt window at a time.

find

Searches through files and command output for the characters you specify.

sort

Alphabetizes files and command output.

To send input from a file to a filter command, use a less than sign (<). If you want the filter command to get input from another command, use a pipe (|).

Using the more command

The more command displays the contents of a file or the output of a command in one Command Prompt window at a time. For example, to display the contents of a file called List.txt in one Command Prompt window at a time, type:

more < list.txt

One Command Prompt window of information appears, and then the -- More -- prompt appears at the bottom of the Command Prompt window. To continue to the next Command Prompt window, press any key on the keyboard except PAUSE. To stop the command without viewing more information, press CTRL+C.

You can use the more command when you work with a command that produces more than one Command Prompt window of output. For example, suppose you want to view a directory tree on your hard disk. If you have more directories than can be displayed in the Command Prompt window, you can use the tree command with a pipe (|) and the more command as follows:

tree c:\ | more

The first Command Prompt window of output from the tree command appears, followed by the -- More -- prompt. Output pauses until you press any key on the keyboard, except PAUSE.

Using the find command

The find command searches files for the string or text that you specify. Cmd.exe displays every line that matches the string or text that you specify in the Command Prompt window. You can use the find command either as a filter command or a standard Windows XP command. For more information about using find as a standard command, see Find 

To use find as a filter command, you must include a less than sign (<) and the string or text on which you want to search. By default, find searches are case-sensitive. For example, the following command finds occurrences of the string "Pacific Rim" in the file Trade.txt:

find "Pacific Rim" < trade.txt

The output does not include any occurrences of "pacific rim." It includes occurrences of the capitalized "Pacific Rim" only.

To save the output of the find command rather than display it in the Command Prompt window, type a greater than sign (>) and the name of the file where you want to store the output. For example, the following command finds occurrences of "Pacific Rim" in the Trade.txt file and saves them in Nwtrade.txt:

find "Pacific Rim" < trade.txt > nwtrade.txt

Using the sort command

The sort command alphabetizes a text file or the output of a command. For example, the following command sorts the contents of a file named List.txt and displays the results in the Command Prompt window:

sort < list.txt

In this example, the sort command sorts the lines of the List.txt file into an alphabetical list and displays the results without changing the file. To save the output of the sort command rather than display it, type a greater than sign (>) and a file name. For example, the following command alphabetizes the lines of the List.txt file and stores the results in the Alphlist.txt file:

sort < list.txt > alphlist.txt

To sort the output of a command, type the command, type a pipe (|), and then type sort (that is, command | sort). For example, the following command sorts the lines that include the string "Jones" (that is, the find command output) in alphabetical order:

find "Jones" maillst.txt | sort