lets say i have a file1 that contains : Dave,7348389800 Bob,3131234567 Harry,248
ID: 3807492 • Letter: L
Question
lets say i have a file1 that contains :
Dave,7348389800
Bob,3131234567
Harry,2483445576
John,3134491390
Sue,2484962204
Marry,6165564458
and another file2 that contains:
Dave,tuesday
Bob,friday
Harry,sunday
John,wednesday
Sue,thursday
Marry,saturday
i need to use a loop in unix to sort them into another file in this way:(by the day of the week)
SUNDAY Harry (248) 496-2204
MONDAY Nobody
TUESDAY Dave (313) 123-4567
WEDNESDAY John (616) 556-4458
THURSDAY Sue (734) 838-9800
FRIDAY Bob (248) 344-5576
SATURDAY Marry (313) 449-1390
SUNDAY Harry (248) 496-2204
Explanation / Answer
awk '
BEGIN {
FS=","
split("MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY", days);
}
NR == FNR { day[$1] = $2; next }
{ lines[toupper(day[$1])] = toupper(day[$1]) OFS $1 OFS $2 }
END {
for (i=1; i<=7; i++) {
if (i==7) {
if (lines[days[i]])
print lines[days[i]]
else
print days[i],"Nobody"
}
}
for (i=1; i<=7; i++) {
if (lines[days[i]])
print lines[days[i]]
else
print days[i],"Nobody"
}
}
' file2 file1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.