Ch 9 Pipes, Filters, and Redirection



Chapter 9

Pipes, Filters, and Redirection

LECTURE NOTES

|CHAPTER OUTLINE |TEACHING SUGGESTIONS |

|Chapter Overview | |

| |Slides 2-6 |

|Will use redirection to redirect standard input and standard output. | |

|Learn how pipes can be used to connect programs. |Chapter Overview duplicated in PowerPoint slides. |

|How filters can be used to manipulate data will be explained. |After completion of lecture, suggest to students that they review |

|What shell extensions are and how they may be used will be discussed. |Objectives and Outcomes found on first page of chapter as a check to |

|Learn how to combine commands using pipes, filters, and redirection. |see if they have mastered concepts. |

| | |

| | |

|REDIRECTION OF STANDARD I/O (INPUT/OUTPUT) | |

|Redirection of Standard I/O (Input/Output) | |

|What is keyed in is input. | |

|What is written to screen is output. |SECTION 9.1 (pp. 440-441) |

|Input/output called I/O. |Slides 7-13 |

|OS recognizes three standards: | |

|Standard input: Keyboard. |Discussion Question (1) - Explain redirection. |

|Standard output: Screen. |Discussion Question (2) - Explain the terms standard input, standard |

|Standard error: place from which OS writes error messages to the screen. |output, and standard error. |

|Not all commands deal with standard input and standard output. |Discussion Question (3) - Does every operating system command use |

|See PowerPoint slide #10 – Results of Copy Command |standard input and standard output? If not, why not? |

|I/O redirection allows standard I/O to be changed. |Discussion Question (4) - What is the difference between > and >> when |

|Read from file, not keyboard. |redirecting output? |

|Write to printer, not screen. |Discussion Question (5) - Explain how the symbol < is used. |

|Three redirection operators. | |

|The > redirects output of command to device or file. | |

|The < asks for input from source other than the keyboard. | |

|The >> redirects standard output and appends but does not overwrite a | |

|file. | |

| | |

|ACTIVITY—USING > TO REDIRECT STANDARD OUTPUT | |

|Using > to Redirect Standard Output | |

|DATA disk in Drive A, A: \> displayed. | |

|Activity steps. | |

|Key in: | |

|DIR C:\WUGXP\*.TXT | |

|DIR C:\WUGXP\*.TXT > TXTFILES.TXT | |

|TYPE TXTFILES.TXT | |

|Activity completed. | |

| |SECTION 9.2 (pp. 441-443) |

| |Slide 14 |

| | |

| |Redirection is either/or process - See on screen, or prints to printer.|

| |Redirection can be used with commands that writes its results to |

| |standard output device (screen). |

| |Cannot use COPY DIR *.TXT - COPY copies files, not commands. |

| |Discussion Question (6) – Keying in COPY DIR filename will not give you|

| |a file containing the directory display. Why? |

|ACTIVITY—USING < TO REDIRECT STANDARD INPUT | |

|Using < to Redirect Standard Input | |

|DATA disk in Drive A, A: \> displayed. | |

|Activity steps. |SECTION 9.3 (pp. 443-445) |

|Key in: |Slide 15 |

|MD TEST | |

|COPY C:\WUGXP\*.NEW TEST |DEL *.* needs keyboard response. |

|DEL TEST\*.* |Useful to lock up the system - i.e., write a file with X , then |

|N then DIR TEST |write command as DEL \TEST\*.* < X.FIL. System will lock up. |

|TYPE Y.FIL |Be careful with redirection of input. |

|DEL TEST\*.* < Y.FIL |- Tell OS to take input from a file; any keyboard input will be |

|DIR TEST |ignored. |

|Activity completed. |. |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

|ACTIVITY—USING >> TO ADD REDIRECTED OUTPUT TO A FILE | |

|Using >> to Add Redirected Output to a File | |

|DATA disk in Drive A, A: \> displayed. | |

|Activity steps. | |

|Key in: |SECTION 9.4 (pp. 445-447) |

|COPY C:\WUGXP\*.TXT |Slide 16 |

|TYPE JUPITER.TXT | |

|TYPE MERCURY.TXT |Can only use >>. There is no JUPITER.TXT |>> between files – adds contents of first file to end of second file. |

|TYPE JUPITER.TXT |To append to end of existing file, use double redirection symbol (>>) |

|Activity completed. | |

| | |

|FILTERS | |

|Filters | |

|Manipulate information. | |

|Read information from keyboard. | |

|Change input in specified way. | |

|Write results to the screen. | |

|Three OS filters - external commands. |SECTION 9.5 (p. 447) |

|SORT - arranges lines in ascending or descending order. |Slides 17-20 |

|FIND - searches for particular group of characters, called a character | |

|string. |Compare filters to filters in water purification system. |

|MORE - temporarily halts screen display after each screenful. |- Extract unwanted elements. |

|OS creates temporary files while it "filters" data. |- Send pure water on its way. |

|Important that there be access to the disk and the filters. | |

|If a disk is write-protected, the OS will not be able to execute filter |Discussion Question (7) - What are filters? |

|commands. | |

| | |

| | |

| | |

| | |

|THE SORT COMMAND | |

|The SORT Command | |

|Sorts/arranges lines of text. | |

|Sends output to screen unless redirected. | |

|Default sorts in ascending order. | |

|SORT syntax: See PowerPoint slide #22. | |

|/R - reverses sort order- sorts Z to A, 9 to 0. | |

|/+n - sorts file according to characters in column n. | |

|M kilobytes – amount of main memory to use for sort | |

|/T – (Temporary) – path of directory to hold sort’s working storage in | |

|case does not fit into main memory. | |

|/O – Output – if not specified data written to standard output. | |

|Specifying output file faster than redirecting standard output to same | |

|file. | |

|[drive1:] [path1] filename1 - specifies file (s) to be sorted. |SECTION 9.6 (p. 447) |

|[drive2:] [path2] filename2 - specifies a file where the sorted input is |Slides 21-22 |

|to be stored. | |

| |Sorts A to Z or from lowest to highest numbers (starting in first |

|ACTIVITY—USING SORT |column). |

|Using SORT | |

|DATA disk in Drive A, A: \> displayed. |Discussion Question (8) – What do the three SORT parameters covered in |

| means to press the key. |this chapter—/n, /O, and /R—represent? |

|Activity steps. | |

|Key in the following commands. Each command followed by | |

|SORT then MECURY then VENUS then EARTH then 3 then MARS | |

|JUPITER then < F6> then SORT then 333 then 3 then 22 then 124 | |

|then | |

|Discuss results of this sort– sorting by units not numerically. | |

|Key in the following commands. Each command followed by | |

|SORT then 333 3 then 23 then 124 then | |

| | |

|Discuss result of this sort – using spaces forces lines to be same length| |

|– placing number digits in proper position. | |

|Activity completed. | |

| | |

|FILTERS AND REDIRECTION | |

|Filters and Redirection | |

|Standard output of filters is screen display. | |

|Can redirect both the output and the input of the filter commands. | |

|Filter commands not usually used with actual keyboard input. | |

|Filter commands used with input redirected from a file, a device, or | |

|another command. | |

| | |

|ACTIVITY—USING THE SORT COMMAND WITH REDIRECTION | |

|Using the SORT Command with Redirection | |

|DATA disk in Drive A, A: \> displayed. | |

|Activity steps. |SECTION 9.7 (pp. 448-450) |

|Key in: |Slides 23-27 |

|COPY C:\WUGXP\STATE.CAP | |

|SORT < STATE.CAP | same as pressing + Z. |

|SORT STATE.CAP |Numbers can be character data – |

|SORT /R < STATE.CAP |- Phone numbers or zip codes. |

|SORT /+17 STATE.CAP |Numbers - only when mathematical operation is performed on them. |

|SORT /+17 STATE.CAP > SORTED.CAP |Character data sorted from left to right. |

|TYPE SORTED.CAP |Numeric data sorted by units. |

|SORT/ +17 STATE.CAP /O BYCITY.CAP |ASCII sort sequence: |

|TYPE SORTED.CAP |- Punctuation marks (including spaces). |

|TYPE BYCITY.CAP |- Numbers. |

|Activity completed. |- Letters (lowercase then uppercase). |

| |Carolyn Smith before Robert Nesler because SORT command looks at entire|

| |line and Carolyn comes before Robert. |

|The FIND Filter |Left justify character data and right justify numeric data. |

|The FIND Filter. |Discussion Question (9) -Explain how the SORT command works. Describe |

|Searches for specific character string by enclosing it in quotation |any limitations of the SORT command. |

|marks. | |

|Command is looking for exact match, therefore, is case sensitive unless | |

|use /I parameter. | |

| |SECTION 9.8 (p. 450) |

| |Slide 28 |

| | |

| | |

| | |

|FIND syntax: | |

|FIND [/V] [/C] [/N] [/I] "string" [[drive:] [path] filename [ …]] | |

|/V - displays all lines not containing specified string | |

|/C - displays only the count of lines containing a string. | |

|/N - displays line numbers with displayed lines. | |

|/I - ignores the case of characters when searching for the string. | |

|/OFF [LINE] - Do not skip files with offline attribute set | |

|"string" - specifies the text string to find. | |

|[drive:] [path] filename - specifies a file or files to search. |SECTION 9.9 (pp. 450454) |

|If pathname is not specified, FIND searches the text types at the prompt |Slides 29-30 |

|or piped from another command. |/R – sorts in reverse or descending order. |

|FIND command at command line can help find a file based on content. |WXP – SORT does not require < prior to file being sorted. |

| |/+n – sorts by column number . |

|ACTIVITY—USING THE FIND FILTER |Column – on screen is place occupied by one character. |

|Using the FIND Filter |Column number really means character number. |

|DATA disk in Drive A, A: \> displayed. |+17 – seventeenth position in list. |

|PERSONAL.FIL on DATA disk. |/O – stores sorted data in a file (faster than redirection). |

|Must use double quotes. |Till now, changed data not been saved to a file. Now creating a new |

|Activity steps. |file. |

|Key in: |Break down SORT < STATE.CAP |

|FIND "Smith" PERSONAL.FIL |- Can be written to screen (standard output) |

|FIND /V "Smith" PERSONAL.FIL |- Therefore can be directed to file. |

|FND /N "Smith" PERSONAL.FIL |Discussion Question (10) -Identify one place that standard output can |

|FIND /C "Smith" PERSONAL.FIL |be written. |

|FIND /I "Jones” PERSONAL.FIL | |

|FIND “Jones” PERSONAL.FIL /I\I |SECTION 9.10 (p. 454) |

|Activity completed. |Slides 31-34 |

| | |

| |Example - Use TYPE command, FIND command can tell if the word |

| |“indictment” is in a file. |

| |Search option available in Start menu - can search files for text. |

| |Command line not as reliable in WXP as it was in W2K |

| | |

| | |

| | |

| |Discussion Question (11) - What is the purpose of the FIND command? |

|PIPES |Discussion Question (12) - What are four parameters that are used with |

|Pipes |the FIND command and what do they represent? |

|Pipes - output from one program becomes input to next program. |Discussion Question (13) - Why must the character string be enclosed in|

|Not limited to two programs. |quotation marks when using the FIND command? |

|Pipes used with filter commands. | |

|Used to further refine data. | |

|Symbol is broken bar | | |

|Location of pipe not standard | |

|Filter commands. | |

|Commands read and write temporary files to the disk. | |

|Filters are external commands. | |

|OS must be able to access the commands. | |

|All files (even temporary ones) must be named. | |

|Temporary files "hold" data until the next command can process it. | |

|Temporary files are deleted when pipes are done filtering. | |

|Filter commands will not work if a disk is write-protected. | |

| | |

|THE MORE FILTER | |

|The MORE Filter | |

|MORE displays one screenful of data at a time. | |

|Useful when want to read long text file. | |

|Pauses after screen is full. | |

|- Press any key - next screen is displayed. | |

|No more data in file – returns to system prompt. | |

| | |

| |SECTION 9.11 (pp. 454-457) |

| |Slides 35-36 |

| | |

| |Character string – enclosed in quotes |

| |FIND – case sensitive. |

| |Parameters. |

| |/I – ignores case. |

| |/V- search file for anything except what is in quotes. |

| |/N – finds specific line # of each occurrence. |

| |/C – numeric count of # of times specific character string in file. |

|MORE syntax: See PowerPoint slide #43. | |

|[drive:] [path] filename - specifies file (s) to display one screen at a | |

|time. | |

|command-name - specifies a command whose output will be displayed. | |

|/E – enable extended features. | |

|/C – clear screen before displaying page. | |

|/P – expand FormFeed character. | |

|/S – squeeze multiple blank lines into a single line. | |

|Tn – expand tabs to n space (default 8). | |

|Switches can be present in MORE environment. | |

|+n – Start displaying the first file at line n. | |

|files – List of files to be displayed. Files in the list are separated |SECTION 9.12 (pp. 457-458) |

|by blanks. |Slides 37-41 |

|If extended features are enabled – following commands accepted at | |

|–More—prompt |Term “pipe” refers to flow of info. from one command to the next. |

|See PowerPoint slide #44. |Pipes move information the way water pipes move water. |

|Right column describes features |Filter commands. |

| |- Used with any command that uses standard output or input. |

|ACTIVITY—USING THE MORE FILTER |- Transforms data to meet needs. |

|Using the MORE Filter | |

|DATA disk in Drive A, A: \> displayed. |Discussion Question (14) - What are pipes? |

|Activity steps. |Discussion Question (15) - Are there any restrictions on the use of |

|Key in all bold commands. |pipes? What are they? |

|DIR | MORE then Return to System Prompt | |

|DIR | MORE then Return to System Prompt | |

|DIR | MORE then Return to System Prompt | |

|DIR | SORT / +39 | MORE then Return to System Prompt | |

|MORE PERSONAL.FIL then Return to System Prompt | |

|TYPE PERSONAL.FIL | MORE then Return to System Prompt | |

|MORE PERSONAL.FIL /C +20 | |

|MORE SORTED.CAP BYCITY.CAP /C | |

|Press | |

| | |

| | |

| |SECTION 9.13 (pp. 458-459) |

|MORE SORTED.CAP BYCITY.CAP |Slides 42-44 |

|Press | |

|Close all open windows and return to desktop environment (GUI). |MORE can be both redirected and used with a pipe. |

|Activity completed. |Extended features |

| |P n Display next n lines |

|OTHER FEATURES OF MORE |S n Skip next n lines |

|Other Features of MORE |F Display next file |

|If extended features are enabled – have more choices available to you |Q Quit |

|with MORE command. |= Show line number |

|See PowerPoint slide # 45. |? Show help line |

|Command Prompt by default enables shell extensions | Display next page |

|CMD /X - Enables shell extensions | Display next line |

|CMD /Y -Disables shell extensions. | |

|Open Command Prompt window are running a shell. | |

|Shell is command interpreter used to pass commands to operating system. | |

| | |

| | |

|ACTIVITY—USING THE EXTENDED FEATURES OF MORE | |

|Using the Extended Features of MORE | |

|DATA disk in Drive A, A: \> displayed. |Discussion Question (16) - How is the MORE command used? |

|Activity steps. | |

|Click Start/Run. | |

|Key in: CMD.EXE /X | |

|Click OK. | |

|Key in: A: then MORE PERSONAL.FIL. | |

|Press twice then Q. | |

|Key in: MORE PERSONAL.FIL | |

|Key in: P then 5 | |

|Press then = sign then S | |

|Key in: 3 then press | |

|Close all open windows. | |

|Activity completed. | |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

| | |

|COMBINING COMMANDS WITH PIPES AND FILTERS | |

|Combining Commands with Pipes and Filters | |

|Use pipes so standard output from one command is standard input to next |SECTION 9.14 (pp. 459-466) |

|command (filters). |Slides 45-46 |

|Use pipes to connect two or more programs and create a flow of data. | |

|Redirecting output from a command is an “instead of” process. |By continuing to press will return to system prompt. |

|When combine use of pipes and > - redirection becomes end of pipeline. |With extended features, pressing Q will break command and return to |

|Last step in process. |system prompt. |

|ACTIVITY—COMBINING COMMANDS |Can connect several commands with pipes and filters. |

|Combining Commands |MORE can be used with any ASCII file. |

|Open Command Prompt window. |/P parameter can only be used with DIR. Demonstrate by using TYPE |

|DATA disk in Drive A, A: \> displayed. |PERSONAL.FIL /P. |

|Activity steps. |/P not a valid TYPE parameter. |

|Key in: |Records are lines of information in a data file. |

|FIND "Teacher" PERSONAL.FIL | FIND "CA" |MORE allows viewing file at a specified line or record number. |

|FIND "Teacher" PERSONAL.FIL | FIND "CA" > TEACHER.FIL | |

|TYPE TEACHER.FIL | |

|FIND "PrOfeSSor" PERSONAL.FIL | FIND "AZ" | SORT | |

|Press up arrow once |More filter can be used with more than one file. |

|Use left arrow key until cursor in under P in PERSONAL.FIL |/C parameter clears the screen before each display. |

|Key in /| | |

|Press key then | |

|Key in: |SECTION 9.15 (pp. 466-467) |

|DIR | SORT /+39 | MORE |Slides 47-50 |

|Return to system prompt. | |

|Key in: |Open Command Prompt window – running a shell. |

|DIR | FIND ““ SORT +39 |Commands that use shell extensions: DEL, COLOR, CD, MD, PROMPT, PUSHD, |

|Quotation marks around so command line would not use < and > as |POPD, SET, SETLOCAL, ENDLOCAL, IF, FOR, CALL, SHIFT, GOTO, STARTS, |

|redirection. |ASSOC, and FTYPE |

|Activity completed. |Command name with /? - full details on what can be done with command. |

| | |

| |Discussion Question (17) – What are two useful features that extended |

| |features provides for the MORE command? |

| | |

| |SECTION 9.16 (pp. 467-471) |

| |Slides 51-52 |

| | |

| |Run remembers last command keyed in. |

| |/X – ensures that you are going to be able to use extensions to |

| |commands. |

| |With extensions enabled |

| |Pressing (referred to as in syntax) - displays next |

| |page |

| |Pressing (referred to as in syntax diagram) - displays |

| |next line in file |

| |To exit MORE command key in Q (returned to system prompt). |

| |Press P where MORE stopped – can request how many lines you want to |

| |display. |

| |= sign – displays which line number you are on. |

| |S – asked how many lines you want to skip in your display. |

| | |

| | |

| |SECTION 9.17 (p. 471) |

| |Slides 53-55 |

| | |

| |When pipe symbol used – must be command on both sides of symbol. |

| |Redirection used with “pipeline,” - command does not have to be on |

| |either side of > or >> |

| | |

| | |

| | |

| | |

| | |

| |SECTION 9.18 (pp. 471-474) |

| |Slides 56-57 |

| | |

| |Data in PERSONAL.FIL not changed – searched data so display only those |

| |lines/records that met your requirements. |

| |Because FIND command sends output to screen can redirect output. |

| |Can use same filter more than once in same command line. |

| |Can use filters in combination in same command line. |

| |Pipe. |

| |- Command on either side of pipe. |

| |- Taking standard output of a command and using it as standard input to|

| |next command. |

| |Redirection. |

| |- An “instead of” action. |

| |- Only get one output place. |

| |- Output goes to last place it is directed to go. |

| |Primary use of pipes and filters is to manipulate the standard output |

| |and standard input of commands. |

| |Rarely use pipes and filters to sort or find data in text or data |

| |files. |

| | |

| |Discussion Question (18) – How can combining filters be useful? |

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download