Unix Awk scripting Help appreciated. In this lab, you’ll use 2 folders: An “Inbo
ID: 3915540 • Letter: U
Question
Unix Awk scripting
Help appreciated.
In this lab, you’ll use 2 folders: An “Inbound” from where you’ll be writing your script, and an “Auto” folder where all files that are ready for automatic processing will be placed.
The input file is called sample_enrollment.csv
The contents of the sample_enrollment.csv are as follows:
"EffectiveDate","Status","EmployeeID","ClientID","MemberFirstName","MemberMiddleName","MemberLastName","MemberSSN","DOB_Month","DOB_Day","DOB_Year","Address1","Address2","City","State","ZipCode","AreaCode","HomePhone","Email","Deduction Method","Customer_Defined","Relationship","Primary","FamilyID","UniqueID"
"05/12/2017","Add","534336699","1511","Jon","B","Doe","123456789","3","17","1980","67 NW 67th Street","","Tempe","AZ","85281","","+1 206 8497834","ift383@asu.edu","Employer payroll deduction","","P","Y","9170885","109170885"
"11/25/2017","Drop","534336699","1511","Alpha","","Gaspart","987654321","4","21","1961","7659 E 12th Ave","","Seattle","WA","98117","","+ 206. 369.2073","ift383@asu.edu","Employer payroll deduction","","S","N","9170885","200319606"
"12/11/2017","Change","534336699","1511","Mat","J","Simmon","987123456","10","7","1995","1234 W 17th Street","","Seattle","WA","98106","","+ 206.369.2073","ift383@asu.edu","Employer payroll deduction","","C","N","9170885","200319607"
"03/25/2018","Drop","534336699","1511","Lance","","Peck","123087654","12","2","2010","12 S Pokoma, #15","Bldg 1002","Boulder","CO","76540","","+ 9172701515","ift383@asu.edu","Employer payroll deduction","","C","N","9170885","200319608"
Name the pre-processed file enrollment_.csv
Consider the file sample_enrollment.csv. It is an Enrollment File that must be pre-processed before it can be run automatically. You are to write an AWK script, lab4_1.awk, to do the pre-processing according to the requirements below:
First, change "EffectiveDate" header to "Effective Date".
Then for each of the requirements below, write a function to complete it:
The Effective date for Drop status must be changed to the last day of the month of the associated “Effective Date”. For example, in the sample file, the last record has a Drop status. Its effective date is 03/25/2018. This must be changed to 03/31/2018.
Format the area code and Home phone number fields to 3 digits and 7 digits respectively. For example, in the sample file, the first record has no value for the area code and "+1 206 8497834" for the home phone. This should be changed to Area code = “206”, and Home phone = “8497834”.
Remove all records with “Change” status so that only the “Add” and “Drop” statuses will be processed.
Finally, save the changed contents in a new filename (as indicated above) and place a copy into the “Auto” folder.
Explanation / Answer
NOTE: I have completed the program for your assignment. Please check and let me know if you have any questions. I will acknowledge back with a response within 24 hours. Thanks for your patience.
Code:
Unix Terminal> cat lab4_1.awk
#!/usr/bin/awk -f
function set_last_month(dt){
last_mon[1] = 31; last_mon[2] = 28; last_mon[3] = 31; last_mon[4] = 30;
last_mon[5] = 31; last_mon[6] = 30; last_mon[7] = 31; last_mon[8] = 31;
last_mon[9] = 30; last_mon[10] = 31; last_mon[11] = 30; last_mon[12] = 31;
split(dt, a, "/");
mon = substr(a[1],2);
date = a[2];
if (mon < 9)
m_indx = substr(mon, 2);
else
m_indx = mon
year = substr(a[3], 1,4);
if (year % 4 == 0)
if (year % 100 == 0)
if (year % 400 == 0)
last_mon[2] = 29;
else
last_mon[2] = 29;
$1 = """ mon "/" last_mon[m_indx] "/" year """;
}
function format_area_home(area_code, home_phone){
if (length(area_code) == 2){
gsub(/"/, "", home_phone);
gsub(/+/, "", home_phone);
if (home_phone ~ /./){
split(home_phone, a, ".");
area_code = a[1];
home_phone = a[2] a[3];
}
else{
split(home_phone, a, " ");
area_code = a[2];
home_phone = a[3];
}
gsub(/[ ]+/, "", area_code);
gsub(/[ ]+/, "", home_phone);
$17 = """ area_code """;
$18 = """ home_phone """;
}
}
function to_continue(status){
if (status == "Add" || status == "Drop")
return 0;
else
return 1;
}
BEGIN{
FS = ",";
OFS = ",";
}
NR == 1{
print $0;
}
NR > 1{
status = $2;
gsub(/"/, "", status);
if (to_continue(status))
next;
set_last_month($1);
area_code = $17;
home_phone = $18;
format_area_home(area_code, home_phone);
print $0;
}
END{
}
Unix Terminal>
Code output snapshot:
Unix Terminal> chmod +x lab4_1.awk
Unix Terminal> ./lab4_1.awk sample_enrollment.csv
"EffectiveDate","Status","EmployeeID","ClientID","MemberFirstName","MemberMiddleName","MemberLastName","MemberSSN","DOB_Month","DOB_Day","DOB_Year","Address1","Address2","City","State","ZipCode","AreaCode","HomePhone","Email","Deduction Method","Customer_Defined","Relationship","Primary","FamilyID","UniqueID"
"05/31/2017","Add","534336699","1511","Jon","B","Doe","123456789","3","17","1980","67 NW 67th Street","","Tempe","AZ","85281","206","8497834","ift383@asu.edu","Employer payroll deduction","","P","Y","9170885","109170885"
"11/31/2017","Drop","534336699","1511","Alpha","","Gaspart","987654321","4","21","1961","7659 E 12th Ave","","Seattle","WA","98117","206","3692073","ift383@asu.edu","Employer payroll deduction","","S","N","9170885","200319606"
"03/31/2018","Drop","534336699","1511","Lance","","Peck","123087654","12","2","2010","12 S Pokoma, #15","Bldg 1002","Boulder","CO","76540","","+ 9172701515","ift383@asu.edu","Employer payroll deduction","","C","N","9170885","200319608"
Unix Terminal>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.