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 (UNIX): a) The AWK programming language

ID: 3605710 • Letter: A

Question

Answer the following questions about AWK (UNIX):

a) The AWK programming language focuses on ______ data processing.

b) Assume you have a file called ‘processNumbers.awk’ containing AWK commands. Give the shell command line to execute the AWK commands in this file. Give AWK commands for accomplishing each of the following:

c) Print the total number of lines from a file named ‘total.txt’

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

e) Assume you have a file called ‘items’ that contains a list of items, one item per line. Also assume that on each line, the 4th field on that line contains the price of that item. Some of the items do not have a price listed, and those lines only have 2 fields. Print the total price for all the items that have a price.

f) Exchange the first and the last 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 “Finished!”. Assume the file name is ‘exchange.txt’. HINT: To switch the two fields around, you’ll need to use a temporary variable.

Explanation / Answer

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.