I have two files.
file1:
Dave 734.838.9800
Bob 313.123.4567
Carol 248.344.5576
Mary 313.449.1390
Ted 248.496.2204
Alice 616.556.4458
file2:
Bob Tuesday
Carol Monday
Ted Sunday
Alice Wednesday
Dave Thursday
Mary Saturday
I merged the two files.
file3 should look like this:
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
But I cannot get the weekdays to be in order. How do I go about doing that?
Best Answer
Here's another way to do it (short version, no temporary files):
If you absolutely need the names of the days in caps then:
Long version:
Let's say we have two files,
file1
:and
file2
:We create
file3
with the following content:and then run:
or, in one line:
Output:
How it works:
join <(sort file2) <(sort file1) | sort -k2
- the first two files are joined based on second field then the output is sorted by second column:this is piped to
join -a1 -j2 -o 1.1 2.1 1.3 2.3 -e "Nobody" <(sort -k2 file3) -
to join it withfile3
based on second field;-a1
adds unmatched lines from file3 to the output and-e "Nobody"
replaces missing output fields with"Nobody"
:the result is piped again to
sort -k1n | cut -d' ' -f 2-
to numerically sort the output on 1st field and then remove the 1st field:since this was grouped
{...}
withprintf %s\\n "Name On-Call Phone"
which prints the header, the entire output is then piped tocolumn -t
to prettify it.You could skip one
sort
if file3 is already sorted on 2nd column (e.g. this time with a simple two-column file3):and also assign a phone number to
"Nobody"
e.g.sed 's/Nobody/888.000.8888/2'
:output: