|
|
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.
- The book showed how the sed command can be used to
- change one text string to another in several files
- show strings to the user and ask about making changes
- edit the input to stream to censor web data
- change only the second instance of a word on each line
- To cause sed to change all instances of "red" to "blue" you
could use
- sed -f "red/blue" filename
- sed -f "/red/blue" filename
- sed -e "s/red/blue/" filename
- sed -e "d/red/blue/" filename
- The awk language can "grep" for text like this:
- 'text to match' { print 0 }
- 'text to match' { print $0 }
- "text to match" { print $0 }
- /text to match/ { print $0 }
- An awk program assumes that words on each text file line
- are actually numbered fields
- are strings and cannot be treated as numbers
- are integer values and cannot be considered strings
- are all contained in $*
- Relational operators in awk can look for strings. What does
each phrase mean?
- $2 < 1024 { print $0 }
___________________________________________
- $1 == "486" { print $2 }
___________________________________________
- /486/ { print $4 }
________________________________________________
- $1 ~ /86/ { print $0 }
____________________________________________
- Which statement is correct?
- $1 == "286" printf("Location: "); print $3;
- $1 == "286" ( printf("Location: "); print $3 )
- $1 == "286" { printf("Location: ") print $3 }
- $1 == "286" { printf("Location: "); print $3 }
- The printf command works like the one in C. Write a command to:
- print the third field as a string
___________________________________________________
- print the third field as a string at least 5 characters wide
___________________________________________________
- print the third field as an integer
___________________________________________________
- print the third field as an integer in a field 5 characters wide
___________________________________________________
- The BEGIN statement in an awk program
- marks where an awk loop starts
- marks the start of an input section
- performs its action before the text file is processed
- is not used in awk programming
- An awk program statement has the general format
- statement
- rule { action }
- action { rule }
- if rule: then { action }
- The END statement in an awk program
- is evaluated after each line in the text file
- is evaluated after the last line in the text file
- is evaluated at the end of each loop
- is evaluated on exiting a loop
- User defined variables in awk programs
- must be declared by the user
- are automatically initialized to 0
- are automatically initialized to NULL
- are not allowed in awk programming
- In the sed command, the caret symbol means
- raise the number on the left to a power
- look at the beginning of a line
- insert a user response here
- Forget this thing: go get a life
- In the awk language, the caret can mean
- do not match on the following set
- look at the beginning of a line
- match on the following set
- I told you: go get a life
- To write an awk statement combining tests on two fields
- use OR as a logical operator
- use a single pipe as a logical operator
- use a double pipe as a logical operator
- use a single ampersand as a logical operator
- If you put a backslash before a metacharacter (like $) in awk, it
means
- ignore the metacharacters special meaning
- print the character and then process its special meaning
- apply the metacharacter to the root directory
- pause before proceeding
- If you put a backslash before a non-special character (like t) in
awk, it means
- use a special meaning the character alone does not have
- ignore any further special meanings
- Special? I got your special, right here...
- lookup the meaning in a special/non-special table, except on
Tuesdays...
- Math operators from C can be used in awk. Explain the following:
- /AT/ { ATcomputers++ } _________________________________________________
- $3 > 0 { RAM += $3 } ___________________________________________________
- /TRS/ { valuecomputers-- } _______________________________________________
- /MAC/ { problems = problems ^ 2 } # warning: this is math! _______________________________________________
|