-2

I'm currently working on trying to merge two files together on unix

first file:

Tom 313.455.6786
Deena 313.899.7400
Will 313.845.5633
Nancy 313.676.9445
Kelly 313.611.4242
John 313.908.3858

second file:

Deena Tuesday
Will Monday
Kelly Sunday
John Wednesday
Tom Thursday
Nancy Saturday

I'm writing the script in the korn shell and I need to include for loops and arrays and all that jazz.

This is what I have so far:

#!/bin/ksh
file1=/home/file1
file2=/home/file2
set -A name
set -A phone
set -A day

for input in `cat file1| cut -d " " -f1`
        do
        name=$input
done

for input2 in `cat file1|cut -d " " -f2`
        do
        phone=$input2
done

    for input3 in `cat file2|cut -d " " -f2`
            do day=$input3
            week= "Monday Tuesday Wednesday Thursday Friday Saturday Sunday"
            for day in $week
            do
            echo $day\n
            day=$i
            done
    done
    printf "%s\t%-12s\t%s\n" "Name" "On-Call" "Phone"; join < (sort file1) <
    (sort file2) | tr ' ' '\t'

This scipt is giving me the following output:

Name    On-Call        Phone
Deena   313.899.7400   Tuesday
John    313.908.3858   Wednesday
Kelly   313.611.4242   Sunday
Nancy   313.676.9445   Saturday
Tom     313.455.6786   Thursday
Will    313.845.56334  Monday

That code sorted the files by alphabetizing the names, rather than by the weekdays, which is what I need. Also it's reversing the On-call output with the Phone output.

I need the final file to look exactly like this

Name    On-Call    Phone
Will    Monday     313.845.5633
Deena   Tuesday    313.899.7400
John    Wednesday  313.908.3858 
Tom     Thursday   313.455.6786  
Nobody  Friday     313.555.3454   
Nancy   Saturday   313.676.9445 
Kelly   Sunday     313.611.4242
7
  • 1
    So this is obviously a homework assignment. Gee! Commented Apr 26, 2015 at 1:23
  • It's like a small project. I've already done some work on it, but I can go out there and research for some help. I didn't want to post my script because I'm sure there are so many errors and that it's totally wrong, but I will anyway. I just need to know what I did wrong, and what I should do different. Commented Apr 26, 2015 at 1:51
  • I just saw that post, but none of the answers were scripts in the korn shell. Also I need help with my specific script that I've written. Commented Apr 26, 2015 at 2:08
  • Please edit your post and include the script in your (now deleted) answer. Then, clarify precisely what your requirements are and which part is still giving you trouble, and this can be reopened. Do you really need to do this in a script? All that's needed here is printf "%s\t%-12s\t%s\n" "Name" "On-Call" "Phone"; join <(sort file1) <(sort file2) | tr ' ' '\t' Commented Apr 26, 2015 at 16:47
  • I just edited it, and specified exactly how my output needs to look. thanks. Commented Apr 27, 2015 at 8:14

1 Answer 1

0

With awk it is simpler than with shell:

awk '
  BEGIN { fmt = "%-8s%-11s%s\n" ; printf fmt, "Name", "On-Call", "Phone" }
  NR==FNR { a[$1] = $2 ; next }
  { printf fmt, $1, $2, a[$1] }
' firstfile secondfile

(You can call that program from your Kornshell.)

Note: the order of the output records match the order of the records in secondfile. So to have the records sorted by week-day, adjust the order of the records in secondfile (or otherwise you need a bit more complex a program).

1
  • That's the thing, I can't edit any of the files. I have to merge those exact files to create that specific output in that format. Because my knowledge on scripting is very limited, it's hard to create complex code. Is there anyway I could sort the columns in the both files and than put them into arrays? Then put them into some type of for loop (I'm not exactly sure how to do this). Commented Apr 26, 2015 at 1:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.