FLOPPIX Home | FAQ | Download | Labs | ToC | Back |  Next 

Pipes and Filters


Notes:

  1. Pipes. A pipe takes the output from one command and uses it as input to the next command.  Example:
  2. Remember:
  3. Commands that are used in pipelines to modify the output from other commands are being used as filters. All of these commands can be used on their own as well. Commands that are frequently used in pipelines include: grep, sort, wc
     
  4. wc: Count bytes, words and lines
  5. sort: sort lines in a file
  6. More examples:


Exercises:

  1. Copy the file /etc/services to your own home directory. This file contains a list of the port numbers used for Internet network services.
     
  2. How many lines are in the file.
     
  3. Display all of the lines for tcp services.
    Count the number of lines for tcp services.
     
  4. Display all of the lines for udp services.
    Count the number of lines for udp services.
     
  5. Use the "last" command to display the record of all login sessions.
    What command counts the number of login sessions for floopy?
     
  6. Display a long directory listing for the /etc directory.
    Pipe the output of the long directory listing through a grep command that will only display the entries for directories in /etc.


Questions & Answers:

The file called "commands" contains:
cal     /usr/bin    9416
date    /bin        2860
who     /usr/bin    8692
cat     /bin        18056
lpd     /usr/sbin   42012

  1. What is the output of the pipeline:
     sort -r  commands  |  head  -1
     
    Answer:  who     /usr/bin    8692
     
  2. What is the output of the pipeline:
    sort  -bk3n  commands  |  head  -1
     
    Answer:  date    /bin        2860
     
  3. What is the output of the pipeline:
    grep  ^c  commands  |  wc  -l
     
    Answer:   2
      
  4. What is the output of the pipeline:
    grep  usr  commands  |  wc  -l
     
    Answer: 3
     
  5. What is the output of the pipeline:
    grep  at  commands  |  sort  |  head  -1
     
    Answer: cat     /bin        18056

Copyright © L.M.MacEwan
FLOPPIX Home | FAQ | Download | Labs | ToC | Back |  Next