This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Various Operations using Awk Command – 1”.
1. Which filter apart from perl, is the most powerful?
a) sed
b) awk
c) grep
d) cut
View Answer
Explanation: The awk command made a later entry in the UNIX system. Like sed, it combines features of several filters. It is one of the most powerful filter after perl.
2. Awk filter operates at field level.
a) True
b) False
View Answer
Explanation: awk filter can do several things, it operates on field level and can easily access, transform and format individual fields in a line.
3. Which of the following will be used to print lines containing ‘manager’ in emp.lst?
a) awk ‘/manager/ { print }’ emp.lst
b) awk ‘/manager { print }’ emp.lst
c) awk ‘/manager/ { print } emp.lst
d) awk ‘manager { print }’ emp.lst
View Answer
Explanation: The syntax for using awk is:
awk options ‘selection_criteria { action }’ file(s)
4. The default action if selection_criteria is missing is ____
a) print
b) split
c) print and split
d) no default action
View Answer
Explanation: If the selection_criteria is missing in awk command statement then the default action i.e. print is applied.
5. For pattern-matching, awk uses regular expressions in ____ style.
a) sed
b) grep
c) perl
d) print
View Answer
Explanation: For pattern-matching, awk uses regular expressions in sed-style. For example,
$ awk -F “|” ‘/sa[kx]s*ena/’ emp.lst
6. awk uses ______ for splitting a line into fields.
a) special parameters
b) shell variables
c) env variables
d) command arguments
View Answer
Explanation: awk uses the special parameter $0 to indicate the entire line. It also uses $1, $2, $3 …. to identify fields. For example,
$ awk -F “|” ‘/sales/ { print $2, $4, $6 }’ emp.lst // prints 2nd, 4th and 6th field where pattern is matched
7. Which built-in variable is used by the awk to specify the line numbers?
a) AR
b) NR
c) $$
d) $?
View Answer
Explanation: awk uses the built-in variable NR to specify line numbers. For example, to select lines 3 to 6 use the following command:
$ awk -F “|” ‘NR == 3, NR == 6 { print NR, $2, $4 }’ emp.lst
8. What is the default delimiter used by awk?
a) tab
b) whitespace
c) double space
d) |
View Answer
Explanation: awk is the only filter which uses whitespace as the default delimiter instead of a single space or tab.
9. The printf function uses ___ for string data and ___ for numeric.
a) %f, %l
b) %s, %f
c) %s, %d
d) %s, %s
View Answer
Explanation: awk filter supports most of the formats used by the printf function in ‘C’ language. Here, %s format will be used for string and %d format for numeric.
10. awk doesn’t use $ in evaluation or assignment of variables.
a) True
b) False
View Answer
Explanation: awk allows the use of user-defined variables but without declaring them. awk doesn’t use $ either in evaluation or in the assignment of a variable.
X=”5” print X // prints 5
11. A user-defined variable is initialized to ____
a) zero
b) zero or null string
c) null
d) operator
View Answer
Explanation: A user-defined variable needs no initialization. It is implicitly initialized to zero or null string and awk has a mechanism of identifying the type and initial value of a variable.
12. awk uses ___ operator for concatenating strings.
a) >
b) |
c) *
d) no operator available
View Answer
Explanation: awk provides no operator for string concatenation. To do so, we have to simply put two strings side by side:
x=”sanf” ; y=”com” print x y // prints sanfcom
13. awk uses __ and __ as comparison operators.
a) $$, ^^
b) ||, &&
c) %%, ##
d) ||, @@
View Answer
Explanation: awk uses the && and | | as logical operators in the same sense as used by the C. The following command looks for two strings in third field only using || operator.
$ awk -F “|” ‘$3== “director” || $3== “manager” { printf “%-20s %-12s %d\n”, $2, $6}’ emp.lst
14. awk allows the user to use variables of his own choice.
a) True
b) False
View Answer
Explanation: awk has certain built-in variables like $0, NR, it also permits the user to use variables of his own choice. For example, following command print the name of those directors having salary>6000.
$ awk -F “|” ‘$3== “director” || $6 > 6000 { > count=count+1 >printf “%-20s %d\n”, $2, $6}’ emp.lst
15. Which option is used for reading an awk program from a file?
a) -e
b) -f
c) -i
d) -F
View Answer
Explanation: We can store large awk programs in a separate file and provide them with a .awk extension. For example,
$ cat emp1.awk $3== “director” || $6 > 6000 { count=count+1 printf “%-20s %d\n”, $2, $6} // commands written in emp1.awk are executed on emp.lst $ awk -F “|” -f emp1.awk emp.lst
Sanfoundry Global Education & Learning Series – Unix.
To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Check Unix Books
- Check MCA Books
- Check Computer Science Books
- Practice MCA MCQs
- Apply for Computer Science Internship