CS211 Shell Programming

Review for Fourth Quiz

This is the last review for this course. It will mostly be over sed and awk. Since these two topics have many similarities and differences, be sure you know which you should be thinking about before you answer a question.

  1. The book showed how the sed command can be used to
    1.  change one text string to another in several files
    2.  show strings to the user and ask about making changes
    3.  edit the input to stream to censor web data
    4.  change only the second instance of a word on each line


  2. To cause sed to change all instances of "red" to "blue" you could use
    1.  sed -f "red/blue" filename
    2.  sed -f "/red/blue" filename
    3.  sed -e "s/red/blue/" filename
    4.  sed -e "d/red/blue/" filename


  3. The awk language can "grep" for text like this:
    1.  'text to match' { print 0 }
    2.  'text to match' { print $0 }
    3.  "text to match" { print $0 }
    4.  /text to match/ { print $0 }


  4. An awk program assumes that words on each text file line
    1.  are actually numbered fields
    2.  are strings and cannot be treated as numbers
    3.  are integer values and cannot be considered strings
    4.  are all contained in $*


  5. Relational operators in awk can look for strings. What does each phrase mean?
    1.  $2 < 1024 { print $0 }
      ___________________________________________
    2.  $1 == "486" { print $2 }
      ___________________________________________
    3.  /486/ { print $4 }
      ________________________________________________
    4.  $1 ~ /86/ { print $0 }
      ____________________________________________


  6. Which statement is correct?
    1.  $1 == "286" printf("Location: "); print $3;
    2.  $1 == "286" ( printf("Location: "); print $3 )
    3.  $1 == "286" { printf("Location: ") print $3 }
    4.  $1 == "286" { printf("Location: "); print $3 }


  7. The printf command works like the one in C. Write a command to:
    1. print the third field as a string
      ___________________________________________________
    2. print the third field as a string at least 5 characters wide
      ___________________________________________________
    3. print the third field as an integer
      ___________________________________________________
    4. print the third field as an integer in a field 5 characters wide
      ___________________________________________________


  8. The BEGIN statement in an awk program
    1.  marks where an awk loop starts
    2.  marks the start of an input section
    3.  performs its action before the text file is processed
    4.  is not used in awk programming


  9. An awk program statement has the general format
    1.  statement
    2.  rule { action }
    3.  action { rule }
    4.  if rule: then { action }


  10. The END statement in an awk program
    1.  is evaluated after each line in the text file
    2.  is evaluated after the last line in the text file
    3.  is evaluated at the end of each loop
    4.  is evaluated on exiting a loop


  11. User defined variables in awk programs
    1.  must be declared by the user
    2.  are automatically initialized to 0
    3.  are automatically initialized to NULL
    4.  are not allowed in awk programming


  12. In the sed command, the caret symbol means
    1.  raise the number on the left to a power
    2.  look at the beginning of a line
    3.  insert a user response here
    4.  Forget this thing: go get a life


  13. In the awk language, the caret can mean
    1.  do not match on the following set
    2.  look at the beginning of a line
    3.  match on the following set
    4.  I told you: go get a life


  14. To write an awk statement combining tests on two fields
    1.  use OR as a logical operator
    2.  use a single pipe as a logical operator
    3.  use a double pipe as a logical operator
    4.  use a single ampersand as a logical operator


  15. If you put a backslash before a metacharacter (like $) in awk, it means
    1.  ignore the metacharacters special meaning
    2.  print the character and then process its special meaning
    3.  apply the metacharacter to the root directory
    4.  pause before proceeding


  16. If you put a backslash before a non-special character (like t) in awk, it means
    1.  use a special meaning the character alone does not have
    2.  ignore any further special meanings
    3.  Special? I got your special, right here...
    4.  lookup the meaning in a special/non-special table, except on Tuesdays...


  17. Math operators from C can be used in awk. Explain the following:
    1. /AT/ { ATcomputers++ } _________________________________________________
    2. $3 > 0 { RAM += $3 } ___________________________________________________
    3. /TRS/ { valuecomputers-- } _______________________________________________
    4. /MAC/ { problems = problems ^ 2 } # warning: this is math! _______________________________________________