[Front] [Prev Chapter] [Next Chapter]

Chapter 16 Redirecting Information

Redirecting Output

Redirecting Input

Using Pipes and Filters


Normally, you enter information at the keyboard, which is the standard input device, and the information is sent to your screen, which is the standard output device. You can redirect this flow of information and send it to a place other than your screen, such as a file or printer.

Note that input is simply what you enter at the keyboard, such as a command, while output is the result of that command.

Redirecting Output

Instead of your information being sent to the screen you can send it to a file or print device. To do this you use the redirection symbol >.

For example, to redirect a DIR listing to a file called MYLIST.DOC instead of displaying it on the screen, enter the following:

DIR >MYLIST.DOC

If MYLIST.DOC does not exist, it is created for you. If it does exist, the original is overwritten, so be careful when redirecting to an existing file.

Instead of the filename, you can enter a file specification, if the file to which you want to redirect output is not on the current drive:

DIR >A:\REPORT\MYLIST.DOC

Redirecting Output to or from a Device

You can redirect output to a device rather than a file or command. Typical devices are printers, plotters, and modems.

As discussed in Chapter 4, "Working with DR-DOS," devices are often named according to the communication port on the computer to which they are connected. Printers can be called PRN or LPTn, (where n could be 1, 2 or 3). Table 16-1 lists the device names and shows how they can be used with redirection.
Table 16-1
Device Names and Redirection Use

Device

Description

Use

CON

User console (the keyboard and the screen).

Input and output

AUX or COM1

First serial communication port.

Input and output

COMn

Second and subsequent serial ports where n is 2, 3, or 4, depending on the particular computer.

Input and output

PRN or LPT1

First parallel printer.

Output

LPT2

Second parallel printer.

Output

LPT3

Third parallel printer.

Output

NUL

All output from this device is discarded. No input is returned from the device. It is used to discard unwanted output.

Input and output

For details on how to set up a port for a particular device, refer to the "Command Reference" chapter of DOSBook for a description of the MODE command. The form of the command for redirecting to a device is:

command >device

For example, the following command sends the DIR listing of drive A: to the printer rather than to the screen:

DIR A: >PRN <Enter>

Appending Output

You can append the output of a command to the end of a file, using the redirection symbol >>. For example, to add a file INDEX.DAT to the end of an existing file called BOOK.TXT, enter:

TYPE INDEX.DAT >>BOOK.TXT

BOOK.TXT now contains the contents of both files.

Redirecting Input

Sometimes it is useful to make the contents of a file the input for a command. You achieve this by using the redirection symbol <.

For example, to make a file called TELEPH.DOC the input to the SORT command (which sorts the contents of a file into alphabetical order), enter

SORT <TELEPH.DOC

SORT takes the file, sorts its contents alphabetically, and outputs the result to the screen.

Taking this a step further, to store the output of SORT into a file instead of just displaying it on screen, enter

SORT <TELEPH.DOC >TELELIST.DOC

SORT takes the TELEPH.DOC file, sorts it into alphabetical order, and stores the result in the file TELELIST.DOC.

Using Pipes and Filters

Pipes

Sometimes you may find it convenient to use the output from one program as the input to another. For example, the DIR command often produces a listing so long that all of it cannot fit on the screen. In this case, you can connect the output of DIR to the input of the MORE command, which displays output one screenful at a time, by using a pipe. The piping symbol is | (a vertical bar). You would invoke the DIR and MORE commands together as follows:

DIR | MORE

You can pipe as many commands as you like. Piping is often used with filter commands (MORE is a filter command).

NOTE: When you pipe programs together, the data piped between them is stored by the operating system in temporary files it creates on the default disk; there must be sufficient space on the disk for them and the disk should not be write-protected. The temporary files are removed when the operation is complete.

Filters

The operating system has several commands that act as filters and can be piped together. A filter command reads your input, acts upon it in some way, and then outputs the result, usually to the screen. The following table lists the main filter commands.
Command

Function

SORT

Arranges data into alphabetical order before displaying it on the screen.

MORE

Displays the contents of a file a screenful at a time.

FIND

Extracts specified items of data to display.

For example, SORT is often used with the DIR command to sort the listing produced, by filename or by file size for example. Entering the following in the command line sorts the DIR listing in reverse alphabetical order; the /R switch causes sorting from last to first rather than from first to last.

DIR | SORT /R

The following command sorts a file called NAMELIST.DOC using SORT. SORT arranges the contents of the file into alphabetical order before it is displayed on the screen:

SORT <NAMELIST.DOC <Enter>

Assume NAMELIST.DOC contains the following names:

HENRY

SALLY

JAMES

SUE

FRED

The SORT command would therefore arrange them into alphabetical order as follows:

FRED

HENRY

JAMES

SALLY

SUE

In the following command line, DIR and FIND are piped together to search for (FIND) and list (DIR) a directory called TEMP:

DIR | FIND TEMP <Enter>

Combining Pipes with Redirection Symbols

You can also use piping and redirection together. For example:

DIR | SORT >DIRSORT <Enter>

This command produces a directory listing which is sorted into alphabetical order and then stored in DIRSORT.

The following command sorts a file called TELE.LST into alphabetical order and displays the result a screenful at a time:

SORT <TELE.LST | MORE <Enter>



[Front] [Prev Chapter] [Next Chapter]

info@caldera.com
Copyright © 1993, 1997, 1998 Caldera, Inc. All rights reserved.