Unix Questions and Answers – Various Operations using Awk Command – 2

This set of Unix online quiz focuses on “Various Operations using Awk Command – 2”.

1. ____ section can be used if we’ve to print something before processing the first line.
a) end
b) begin
c) start
d) first
View Answer

Answer: b
Explanation: awk statements are usually applied to all the lines selected by the address. But if we want to print something before processing the first line, for example, a heading, then the BEGIN section can be used. The syntax is:

BEGIN 
{ 
   action 
}

2. ____ section can be used if we’ve to print something after processing the last line.
a) end
b) begin
c) start
d) first
View Answer

Answer: b
Explanation: awk statements are usually applied to all the lines selected by the address. But if we want to print something after processing the first line, for example, a footer, then the END section can be used. The syntax is:

advertisement
advertisement
END 
{ 
    action 
}

Note: Join free Sanfoundry classes at Telegram or Youtube

3. awk uses ____ for providing comments.
a) %%
b) //
c) #
d) $$
View Answer

Answer: c
Explanation: Like shell, awk also used the # symbol for providing comments. For example,

BEGIN 
{
    printf “\t\t employee data\n\n”
}              # demonstration of BEGIN section - (this is a comment line)

4. Which of the following is a built-in variable for awk?
a) ps
b) $0
c) FS
d) FN
View Answer

Answer: c
Explanation: awk has several built-in variables like FS, OFS, NF each of which serves a different purpose.
advertisement

5. FS defines the field separator.
a) True
b) False
View Answer

Answer: a
Explanation: As we know that, awk uses a contiguous set of space as the default field delimiter. FS defines this field separator. For example,

advertisement
BEGIN { FS= “|” }         // | is the delimiter now

6. Which built variable is used for defining the field delimiter in the output?
a) OFS
b) FS
c) NF
d) ES
View Answer

Answer: a
Explanation: The OFS variable is used for specifying the field delimiter in the output. For example,

BEGIN { OFS= “~” }    // output fields will be separated with a tidle.

7. The ____ variable stores the name of the file currently being processed.
a) OFS
b) FILENAME
c) NF
d) ES
View Answer

Answer: b
Explanation: awk supports another built-in variable named FILENAME which stores the name of the file, currently being processed.

8. Which of the following is not a built-in variable used by awk?
a) ES
b) FILENAME
c) argv
d) argc
View Answer

Answer: a
Explanation: argv is used for storing the list of arguments while argc stores the number of arguments in the command line.

9. awk supports the use of arrays.
a) True
b) False
View Answer

Answer: a
Explanation: awk supports the use of arrays but awk arrays differ from those arrays used in another programming language. These arrays are accessed normally using an index.

10. Which of the following is not true about awk arrays?
a) they are not expanded automatically
b) they are initialized to zero or an empty string
c) index can be a string also
d) they are not formally defined
View Answer

Answer: a
Explanation: awk supports the use of arrays but awk arrays differ from those arrays used in another programming language. awk arrays are expanded automatically.

11. Which of the following is an associative array?
a) a[ ]             
b) arr[ ]            
c) envr [ ]            
d) ENVIRON[ ]            
View Answer

Answer: d
Explanation: awk doesn’t treat array indices as integers. Awk arrays are associative (hash) arrays. Similarly, ENVIRON[ ] is also an associative array, known as environment array which is used to store all environment variables.

Sanfoundry Global Education & Learning Series – Unix.

To practice all areas of Unix for online Quizzes, 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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.