0

Good afternoon everyone, I have a project I am working on, but I am having a bit of trouble with it. I am supposed to create a script that uses a loop structure to output data from 2 data files.

These are the two data files:

data1:

Dave,7348389800
Bob,3131234567
Carol,2483445576
Mary,3134491390
Ted,2484962204
Alice,6165564458

data2:

Bob,tuesday
Carol,friday
Ted,sunday
Alice,wednesday
Dave,thursday
Mary,saturday

This is how it is supposed look when i display it:

Day       Name     Phone
__________________________________________
MONDAY     Nobody
TUESDAY    Bob    (313) 123-4567
WEDNESDAY  Alice  (616) 556-4458
THURSDAY   Dave   (734) 838-9800
FRIDAY     Carol  (248) 344-5576
SATURDAY   Mary   (313) 449-1390
SUNDAY     Ted    (248) 496-2204

This is my current code:

#!/bin/ksh
for day in monday tuesday wednesday thursday friday saturday sunday
    do
    day=`grep -i day data2 |cut -d "," -f 2 `
    name=`cut -d "," -f 1 data1 `
    phone=`cut -d "," -f 2 data1`
done
echo  $day $name $phone >>output

And this is the output I am getting:

Day Name Phone
==============
tuesday friday sunday wednesday thursday saturday Dave Bob Carol Mary Ted Alice 7348389800 3131234567 2483445576 3134491390 2484962204 6165564458

Any help would be greatly appreciated!

10
  • 1
    Don't use the same variable day for both the iteration and the variable inside the loop; Commented Nov 20, 2015 at 20:56
  • 1
    grep -i day should be grep -i $day Commented Nov 20, 2015 at 20:57
  • You are echoing after the loop not in it so you only get to create output once. You aren't quoting your variables so the newlines in the values are being lost (you don't want the newlines in the first place so this is a second-order problem). You aren't selecting individual lines from the files in the loop you are getting the fields from every line every time. Commented Nov 20, 2015 at 20:57
  • Look on this site for questions about merging files based on column fields. There are many of them. Commented Nov 20, 2015 at 20:58
  • Ok I'm updating the code now. Thank you for the replies. Commented Nov 20, 2015 at 21:00

2 Answers 2

1

This works, using printf to get neatly formatted columnar output:

format="%-9s  %-9s  %s\n"

printf "$format" Day Name Phone
printf "$format" Day Name Phone | sed 's/./-/g'

for day in monday tuesday wednesday thursday friday saturday sunday
do
    who=$(grep -i "$day" data2 | cut -d "," -f 1)
    if [ -z "$who" ]
    then
        who="NOBODY"
        phone=""
    else
        phone=$(grep -i "$who" data1 | sed 's/^[^,]*,\(...\)\(...\)\(....\)/(\1) \2-\3/')
    fi
    printf "$format" $(echo "$day" | tr 'a-z' 'A-Z') "$who" "$phone"
done

You could investigate typeset -u uday="$day" and then pass "$uday" to the printf; that doesn't seem to be an option in Bash 3.x, but it works in Korn shell and in Bash 4.x (for big enough values of x). […Thinking about it, the grep | cut and grep | sed sequences could (and probably should) be replaced by pure sed, which is tidier still. You could also avoid the repeated printf to deal with the heading by using sed 'p;s/./-/g'. Fixing those issues is left as an exercise for the reader…]

Output:

Day        Name       Phone
---------------------------
MONDAY     NOBODY     
TUESDAY    Bob        (313) 123-4567
WEDNESDAY  Alice      (616) 556-4458
THURSDAY   Dave       (734) 838-9800
FRIDAY     Carol      (248) 344-5576
SATURDAY   Mary       (313) 449-1390
SUNDAY     Ted        (248) 496-2204
Sign up to request clarification or add additional context in comments.

8 Comments

Nicely done -- and a heck of a lot shorted than using a while loop... e.g. while read -u 4 line1 && read -u 5 line2; do <stuff>; done 4< "$1" 5< "$2".
WOW! I can't thank you enough man. This works perfectly. The only problem is {format="%-9s %-9s %s\n"} shows up between each row. I'm gonna look over my code, maybe I missed something. I can't thank you enough though. Sadly, my reputation is too low, so I can't upvote your answer.
If you copied my code verbatim, I don't see how that can happen. I omitted the shebang, but that shouldn't be a factor in it. Did you see curly braces too? (You can include code in a comment by enclosing it in back quotes `` code ``, but getting back quotes into a comment is horrifyingly painful; I keep forgetting how to do it reliably.)
You used print instead of printf near the end. You only need one set of I/O redirection; wrap the whole script with { at the top and } > project2.output at the bottom (both on lines of their own). Or better, just run the script and redirect its output: ksh schedule.sh > project2.output. Don't put unnecessary redirections in the code.
I'm having trouble with with pasting the code, but no, it doesn't have those curly braces. I only included those because I thought that was how you input code.
|
0

awk to the rescue!

$ awk -F, -vOFS="\t" '
     BEGIN{split("1monday 2tuesday 3wednesday 4thursday 5friday 6saturday 7sunday",days," ")} 
   NR==FNR{a[$1]=$2;next} {b[$2]=$1} 
       END{for(i in days) {
            d=substr(days[i],2,length(days[i])-1); 
            dp=toupper(days[i]); 
            if(b[d]) 
               print dp,b[d],a[b[d]]; 
            else 
               print dp,"Nobody"}}' data1 data2 
 | sort | cut -c2- | column -t 
 | sed -r 's/([0-9]{3})([0-9]{3})([0-9]{4}$)/(\1) \2-\3/'


MONDAY     Nobody
TUESDAY    Bob     (313) 123-4567
WEDNESDAY  Alice   (616) 556-4458
THURSDAY   Dave    (734) 838-9800
FRIDAY     Carol   (248) 344-5576
SATURDAY   Mary    (313) 449-1390
SUNDAY     Ted     (248) 496-2204

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.