0

This question already has an answer here:

I have a project I am working on that is very similar to the one below i just found so i don't want to make a hassle for anyone to read again, however in the question I found the top answer user had to create an extra file called file3, which is something I don't want to make. I was also thinking about the grep command, also thought about using a while loop. Anyone has any ways to start this script?

how to sort by the day of the week?

In other words, the question is: give an alternate solution to this answer without resorting to creating any extra file (like file3 in the example).

marked as duplicate by Archemar, don_crissti, roaima, Community Mar 22 '17 at 21:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    The second answer on your referenced question doesn't require the intermediate file. I don't see why your question here isn't a duplicate. – roaima Mar 21 '17 at 16:53
1

Sometimes there are nice general solutions, and sometimes, when there's 7 fixed days of the week, it's easy and obvious to hard-code it.

Given an input file (from the linked question) of:

Name      On-Call     Phone
Carol     MONDAY      248.344.5576
Bob       TUESDAY     313.123.4567
Alice     WEDNESDAY   616.556.4458
Dave      THURSDAY    734.838.9800
Nobody    FRIDAY      634.296.3356
Mary      SATURDAY    313.449.1390
Ted       SUNDAY      248.496.2204

The following "one-liner" will sort it by day of the week:

$ { sed -n 1p input; 
    for d in SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY
    do grep " $d " input || printf "%-10s%-12s888.000.8888\n" "Nobody" "$d"; done }
Name      On-Call     Phone
Ted       SUNDAY      248.496.2204
Carol     MONDAY      248.344.5576
Bob       TUESDAY     313.123.4567
Alice     WEDNESDAY   616.556.4458
Dave      THURSDAY    734.838.9800
Nobody    FRIDAY      634.296.3356
Mary      SATURDAY    313.449.1390

The { ... } curly braces group the commands together (so that you can redirect the output to a new file if you want); the sed command prints the header; and the for loop gives grep each day in the order that you want them.

This takes advantage of the fact that you have a hard-coded set of names for the day of the week and also that none of those days match any of your field names in the header. If the header might match, you could just change the inner grep to:

sed 1d input | grep " $d "

Another option, to be more precise about matching the day of the week in the second column, is (for the inner loop):

awk -v day=$d '$2 == day' input
  • The problem is getting the days from the file WITHOUT having to hardcode it. For example i change one of the days in the file, I don't think it will come back as the same variable does it ? – Ali Soujod Mar 28 '17 at 14:35
  • If there are no "Monday" lines, then there'll be no "Monday" output. If you change a "Monday" to a "Tuesday", then that'll show up in the Tuesday grep. Can you show an example of how this breaks? – Jeff Schaller Mar 28 '17 at 14:39
  • That would exactly be my main question, I have a file with all the days BUT monday, now i don't want to HARDCODE but also want to assign the monday to "Nobody"! EDIT : Days Also needed to be in order ! – Ali Soujod Mar 28 '17 at 15:08
  • Ahh! I missed that part of the linked question. I've adjusted this answer to insert "Nobody" (with an arbitrary phone number) if there's noone in the input file for a particular day. – Jeff Schaller Mar 28 '17 at 15:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.