250+ TOP MCQs on Various Operations using Awk Command

Unix Multiple Choice Questions on “Various Operations using Awk Command”.

1. Which filter apart from perl, is the most powerful?
a) sed
b) awk
c) grep
d) cut

Answer: b
Clarification: 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

Answer: a
Clarification: 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

Answer: a
Clarification: 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

Answer: a
Clarification: 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

Answer: a
Clarification: 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

Answer: a
Clarification: 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) $?

Answer: b
Clarification: 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) |

Answer: b
Clarification: 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

Answer: c
Clarification: 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

Answer: a
Clarification: 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

Answer: b
Clarification: 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

Answer: d
Clarification: 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) ||, @@

Answer: b
Clarification: 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  %dn”, $2, $6}’ emp.lst

14. awk allows the user to use variables of his own choice.
a) True
b) False

Answer: a
Clarification: 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  %dn”, $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

Answer: b
Clarification: 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  %dn”, $2, $6}
 
// commands written in emp1.awk are executed on emp.lst
$ awk  -F “|” -f emp1.awk  emp.lst

Leave a Reply

Your email address will not be published. Required fields are marked *