Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Answer the following questions about AWK: a) The AWK programming language focuse

ID: 3706819 • Letter: A

Question

Answer the following questions about AWK:

a) The AWK programming language focuses on what type of data processing?

b) Assume you have a file called ‘calculate.awk’ containing AWK commands. Give the command for running all of the commands in this file.

Give AWK commands for accomplishing each of the following:

c) Print the 2nd last field (the field directly before the last field) of each line from a file named ‘last.txt’

d) Assume you have a file called ‘names’ that contains a list of people, one person per line. Also assume that on each line, the 3rd field on that line contains the age of the person. Some of the people do not have an age listed, and those lines only have 1 field. Print the total age for all the people that have an age (the sum of all of their ages).

e) Print the total number of lines from a file named ‘lines’

f) Exchange the second and the third fields of every line, and then print the lines (you can print each line directly after the exchange). After printing all of the lines, print out the message “Done!”. Assume the file name is ‘switch.txt’.

Explanation / Answer

Let me know if you have any doubts.

Answer a)

The AWK programming language focuses on TEXT data processing
Explanation: awk is generally used for text processing.

Answer b)

awk -f processNumbers.awk
Explanation: -f indicates for file here

Answer c)

awk 'END{print NR}' total.txt
Explanation: NR is the number of records which means total number of lines in the file

Answer d)

awk '{ print ( $(NF-1) ) }' second.txt
Explanation: NF is the total number of fields in the records. NF-1 means second last column/field.

Answer e)

awk '{ sum += $4} END { print sum}'
Explanation: $4 is the fourth column and sum with add each row fourth column value.