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

a) What is AWK, and why is it useful? b) Assume you have a file called ‘calculat

ID: 3816798 • Letter: A

Question

a) What is AWK, and why is it useful?

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

c) Give AWK commands for accomplishing each of the following: - Print the 2nd last field (the field directly before the last field) of each line from a file named ‘last.txt’ - 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). - Print the total number of lines from a file named ‘lines’ - Exchange the first 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’. HINT: To switch the two fields around, you’ll need to use a temporary variable

Explanation / Answer

A)

Awk:

Awk was created in 1970s, at Bell labs.

It is a Programming language which is used in most of the unix and similar operating systems.

It is mainly designed for text processing and can also be used as extraction and reporting tool of data.

It can solve complex text processing tasks with a very few lines of code.

The main advantage of AWK is that it can be used for calling the script, if we have coded the script then it can be used repeatedly as no of times required.

3Types of AWK:

AWK, NAWK, GAWK.

Used for Textprocessing, getting formatted text reports, doing arithmetic operations, string operations etc..

B)

AWK commands can be executed directly from the command line, or in a text file containing AWK commands.

Now the command used for executing all the commands in Calculate.awk file is

awk -f calculate.awk file...

"file..." indicates the text file on which the commands should be executed.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote