Using the awk command in Unix. I need to manipulate raw data with course numbers
ID: 3599292 • Letter: U
Question
Using the awk command in Unix.
I need to manipulate raw data with course numbers(the numbers around the dash), the 5 digit catalog number and 9 digit employee numbers at the end like the following two examples.
Fall 2017 COMM 438T-01 21785 888972491
Summer 2017 MSW 571-40 11230 890248716
Produce a listing which each entry must have an employee number. The listing is grouped by "Season" and order by class number and then section number in descending order. At the end of the report, provide total number of class count for each "Season". And finally, provide the total number of count which a class does not have a valid 9 digit employee number.
I need to do this with awk by reading in a text file with the program in it.
Explanation / Answer
Code:
Unix Terminal> cat find_invalid.awk
#!/usr/bin/awk -f
BEGIN{
total_invalid = 0;
}
{
# flag to identify if the line is invalid format
is_invalid = 0;
# err_mesg variable to capture the invalid format reason
err_mesg = "";
# capturing season information and validating it
season = $1;
if(season !~ /Fall|Summer/){
is_invalid = 1;
err_mesg = "Invalid Season";
}
# capturing year information and validating it
year = $2;
if(length(year) != 4){
is_invalid = 1;
err_mesg = err_mesg " Invalid Year";
}
# capturing course short name and validating it
course = $3;
if(length(course) > 4){
is_invalid = 1;
err_mesg = err_mesg " Course name is more than 4 characters";
}
# capturing course no, section and validating it
split($4, a, "-");
course_no = a[1];
section = a[2];
if(! length(course_no) >= 2 && length(course_no) <= 4){
is_invalid = 1;
err_mesg = err_mesg " Course no is not 2 to 4 digits";
}
if(length(section) > 2){
is_invalid = 1;
err_mesg = err_mesg " Section no is not 2 digits";
}
catalog_course_no = $5;
if(length(catalog_course_no) > 5){
is_invalid = 1;
err_mesg = err_mesg " Invalid/missing catalog course number";
}
if(is_invalid){
$(NF+1) = err_mesg;
print $0;
total_invalid++;
}
}
END{
print "Total Invalid Course names are: ", total_invalid;
}
Unix Terminal>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.