c and c++ sed and gawk
1
CSCE 3600: Systems Programming
Minor Assignment 5 – sed and gawk
Due: Wednesday, November 27, 2019 at 11:59 PM
PROGRAM DESCRIPTION:
In this assignment, you will write sed and gawk commands to accomplish certain
requested functionality. Given the many powerful features of sed and gawk, you are
provided with a link to a tutorial for sed as well as gawk to assist you in completing this
assignment.
Using sed
For help using sed, you may find the tutorial http://www.grymoire.com/Unix/Sed.html or
the actual sed manual https://www.gnu.org/software/sed/manual/sed.html useful.
a) The // character sequence is often known as C++ style or single-line comments,
while the /* … */ character sequence is often known as C-style or multi-line
comments. As an example, assume the following myprog.c file that has a mix of
both C-style and C++ style comments:
// This is a test of how this works #include <stdio.h> int main() { // declare some variables here int num1 = 4; float num2 = 3.5; // print the result printf("The result is %f\n", num1 * num2); // this does it /* does it work? */ return 0; }
Write a one-line sed command that transforms all of the C++ style (i.e., single-line
comments) to C-style (i.e., multi-line comments) and also upper-cases the comment
text so that after running the appropriate sed command, the following would be
output to the terminal:
/* THIS IS A TEST OF HOW THIS WORKS */ #include <stdio.h> int main() { /* DECLARE SOME VARIABLES HERE */ int num1 = 4;
2
float num2 = 3.5; /* PRINT THE RESULT */ printf("The result is %f\n", num1 * num2); /* THIS DOES IT */ /* does it work? */ return 0; }
Write a single sed script called minor5_1.sed.
sed -r -f minor5_1.sed myprog.c
b) Consider the following file called dates.txt containing some birthdays formatted
as <dd> <mm> <yyyy>:
02 01 2002 27 03 2005 19 04 1999 30 11 2007
Write a single sed script called minor5_2.sed that does the following:
1. Swaps the day and month entries of each input
In this file, for example, my sed script should print the following:
$ sed -r -f minor5_2.sed dates.txt 01 02 2002
03 27 2005
04 19 1999
11 30 2007
This sed script file will be submitted to Canvas.
Using gawk
For help using gawk, you may find the tutorial http://www.grymoire.com/Unix/Awk.html
or the gawk manual https://www.gnu.org/software/gawk/manual/gawk.html useful.
a) Consider the following file called sides.txt:
x y z
1 2 3
2 2 3
4 3 5
-1 0 1
Each field in this file is separated by a tab and each record is separated by a newline
character. gawk is a very powerful utility; it can make calculations as well as support
branching statements and function calls. For this part, you will write a gawk program
file where the program and input file are provided as command-line arguments to
3
compute if the supplied numbers on a line of input constitute a valid set of side lengths of a triangle. Recall that a set of values (x,y,z) constitute the sides of a triangle if no side is equal to or larger than the sum of the other two sides. Also, no side dimension should be zero or negative.
For e.g. with the first line (ignoring the header line) above, containing values 1, 2 and 3, the output should be ‘NO’. For the second line above, containing values 2, 2, and 3, the output should be ‘YES’.
The outputs when run with the above data should be:
NO
YES
YES
NO
Your gawk program should be invoked as:
gawk -f minor5_1.gawk sides.txt
b) Consider the following file called grades.txt:
Last First T1 T2 T3 ------- ---- -- -- -- Adams Will 86 77 74 Bell Adam 98 83 Evans Kris 92 87 96 Jackson Ben 68 75 82 Morrow Claire 51 62 66 Pratt Joseph 38 Smith Tommy 99 83 94 Welsh Jenny 62 43 54
Each line (i.e., record) contains the last name, then first name, followed by one to three test scores between 0 and 99 similar to what is shown in the file above. A
newline character separates each record. Write a single gawk program file called
minor5.gawk that will read the grades.txt file and calculate the grades for each
student listed, where 89.5 or greater is an “A”, 79.5 or greater is a “B”, etc. You will print out the entire record and add their grade. If a student is missing two or more scores, however, he/she will automatically earn an “F”. However, if the student is missing at most one score, then the other scores should be used to grade along with
a 0 for the missed score. You will also print out his/her entire record, substituting “--
" in place of the missing field, along with their grade and a note indicating that the student has missing scores. Obviously, you need to skip the first two lines that contain header information.
In this file, for example, the gawk program should print the following:
$ gawk -f minor5_2.gawk grades.txt Adams Will 86 77 74 => C Bell Adam 98 83 -- => D (missing score)
4
Evans Kris 92 87 96 => A Jackson Ben 68 75 82 => C Morrow Claire 51 62 66 => D Pratt Joseph 38 -- -- => F (missing scores) Smith Tommy 99 83 94 => A Welsh Jenny 62 43 54 => F
Formatting properly in columns as shown is required. This gawk program file will be
submitted to Canvas.
REQUIREMENTS:
Your sed script and gawk program files should include your name and EUID at
the top of the file. No other comments are needed in these files.
For the gawk and sed commands, test out your results on real files on our CSE
machines (e.g., cse01, cse02, …, cse06), to make sure that they indeed work.
Your solution to the one-line sed script and gawk program can be typed (or
copied and pasted) to this document and will be submitted to Canvas.
Your program will be graded based largely on whether it works correctly on the
CSE machines (e.g., cse01, cse02, …, cse06), so you should make sure that
your program runs on a CSE machine. Please include any special instructions
required to run your sed script and gawk program.
This is an individual programming assignment that must be the sole work of the individual student. Any instance of academic dishonesty will result in a grade of “F” for the course, along with a report filed into the Academic Integrity Database.
SUBMISSION:
You will electronically submit this file with your typed one-line solutions for sed
and gawk along with your sed scripts minor5_1.sed, minor5_2.sed and
gawk programs minor5_1.gawk and minor5_2.gawk to the Minor 5 dropbox in
Canvas by the due date and time.