Who can help me solve each question. Give AWK commands for accomplishing each of
ID: 3817519 • Letter: W
Question
Who can help me solve each question.
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'.Explanation / Answer
print second last field
awk '{ print $(NF-1)}' last.txt
The sum of all their ages
awk '{sum=sum+$3} END {print sum}' names.txt
explanation:
here $3 is the third field
print total number of lines from a file named 'lines'
wc - l lines.txt
explanation:here wc -l counts the no of lines in a file.
Exchange first and third lines
awk 'BEGIN{FS=" "} {tmp=$1;$1=$2;$2=tmp;print} END{print "done"}' switch.txt
explanation
here FS is field separator.which specefies space is field seperator
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.