hello every one
I have a problem that drove me crazy last couple days I know I have to use loop structures but I can't figure out how to put the right pieces together. I'm not asking for a complete answer because I want to learn so please help me out
The problem says
write a shell script that will take the information from two files and combine into another file
I created both files in vi and they are


file1
David 734.854.5643
Roberto 313.432.4532
Sally 267.423.5412
Mary 435.432.7654
Ted 324.642.6743
Alice 234.576.3245
Frank 342.465.6754

and the second file is


file2
Roberto Tuesday 2
Sally Monday 8
Ted Sunday 16
Alice Wednesday 23
David Thursday 10
Mary Saturday 14
Frank Friday 15


The output file should be like this:
Name----------------On-Call--------------------Phone------------------Start Time
Sally---------------- Monday ---------------267.423.5412-----------------8am
Roberto-------------Tuesday---------------313.432.4532-----------------2am
Alice---------------Wednesday-------------234.576.3245----------------11pm
David---------------Thursday---------------734.854.5643----------------10am
Frank----------------Friday------------------342.465.6754----------------3pm
Mary---------------Saturday-----------------435.432.7654----------------2pm
Ted-----------------Sunday------------------324.642.6743-----------------4pm

I noticed right away that there is a common thing between the two files (the name), and I thought i could use for loop to cut pieces using the day of the week because I have to sorted in a specific order starting from Monday so I thought this loop might work:

for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday
do
grep $day file2
Done

I know it is not complete but I just wanted you to know how I should do the sort. And there is another problem which is the time. in the file is in 24 hours format i have to convert it to 12 hours format.
And that is it for now I want you guys just to tell me how to start and how to put the pieces together.
your help us appreciated
Thank you very much

My first question is whether or not you must do this as a shell script of if you can use something more suited to the task - such as perl, ruby, or python?

Yes I have to use inside the shell
I don't know how to code in perl or ruby or pythone. I just don't get the lohic of how to grep and cut different pieces from the two files and then put them together like a report

Waiting for your help >>

Well my approach would be somthing similar to the following:

sort file1 > sorted1
sort file2 > sorted2
paste sorted1 sorted2 | awk -f process.awk

You'd have to write the conversion from 24hr to 12hr format but in awk that should be fairly straightforward.

The problem is I have to cut and grep and then generate the output using loops I know how to sort by the day of the week using this for loop:

for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday
do
grep $day file2
Done

but the problem is how to get the other information to generate a report that would look like this using loop structure:


The output file should be like this:
Name----------------On-Call--------------------Phone------------------Start Time
Sally---------------- Monday ---------------267.423.5412-----------------8am
Roberto-------------Tuesday---------------313.432.4532-----------------2am
Alice---------------Wednesday-------------234.576.3245----------------11pm
David---------------Thursday---------------734.854.5643----------------10am
Frank----------------Friday------------------342.465.6754----------------3pm
Mary---------------Saturday-----------------435.432.7654----------------2pm
Ted-----------------Sunday------------------324.642.6743-----------------4pm


thank you


waiting your help...

I don't think that cut and grep are the right tools for the job. It makes the solution convoluted. Basically you would have to do something like:

for name in `cut -f1 -d' ' file1`; do 
   # a bunch of grep/cut shenanigans
   echo ${OUTPUT}
done

Or, if you need the output sorted by day, you add another level of ugliness to that.

I'm assuming that this is an assignment to get you exposure to grep and cut . If that is the case use what I've given you and expand - if not use a different method.

If you have problems in the process post what you have issues with and we will try to help.

Ok guys I made the script working but there is one last problem which is the AM and PM

I coded the following if statement:


if [ $Start -gt 12 ]
then
Start=`expr $Start -12` "PM"
else
Start=$start"AM"
fi

but it still gives me an error in this line: Start=`expr $Start -12` "PM"

any suggestions?


Thank you

Instead of

Start=`expr $Start -12` "PM"

try

Start="`expr $Start -12` PM"

Notice the placement of the quotations.

Ok guys I made the script working but there is one last problem which is the AM and PM

I coded the following if statement:

if [ $Start -gt 12 ]
then
     Start=`expr $Start -12` "PM"


There's no need to use an external program if you are using a standard Unix shell (which includes bash, ksh, ash, dash); it has integer arithmetic built in:

Start="$(( $Start - 12 )) PM"

write a Unix shellscripthat will take the information from two files and combine into another file

I created both files in vi and they are


file1

David 734.854.5643

Roberto 313.432.4532

Sally 267.423.5412

Mary 435.432.7654

Ted 324.642.6743

Alice 234.576.3245

Frank 342.465.6754

second file is

file2

Roberto Tuesday 2

Sally Monday 8

Ted Sunday 16

Alice Wednesday 23

David Thursday 10

Mary Saturday 14

Frank Friday 15
The output file should be like this:

Name----------------On-Call--------------------Phone-------Start Time

Sally---------------- Monday ---------------267.423.5412------8am

Roberto-------------Tuesday---------------313.432.4532---------2am

Alice---------------Wednesday-------------234.576.3245-------11pm

David---------------Thursday---------------734.854.5643---------10am

Frank----------------Friday------------------342.465.6754-------3pm

Mary---------------Saturday-----------------435.432.7654-------2pm

Ted-----------------Sunday------------------324.642.6743------4pm


$file1 | sort > file1.sort

$file2 | sort > $file2.sort

echo -e "name\t days\t \phone numbers\t \Time\t"

for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday

name=`cat $file2.sort | grep $Oncall | cut -d, -f1`

if { $name }; then

phone = `cat file1.sort | grep $name | cut -d, -f2`

echo -e "$names\t \days\t \$phonenumbers \$Time

fi

done

Did I do the for loop right??

What else do i need to add to the loop.

how to loop around again??

I know how to do the convert time

Edited 5 Years Ago by bossman5000: n/a

I've already answered you here and here. If you would have spent the time you've spent ignoring me focused on your problem then you may have solved it by now.

What errors are you getting? What solutions have you tried?

Man relax....i dont notice that u reply... Im 2 new this....

I suppose to use grep and cut...

Is the for loop right???
How can it loop around and get the days and phone??

Here is how you construct a loop:

#!/bin/bash

DAYS="Monday Tuesday Wednesday Thursday Friday"

for day in ${DAYS}; do
    echo "DAY: ${day}"
done

If you want to find a particular day in a file (as listed by grep ) you would do something like:

egrep ${day} file2.txt

You could save the output of that to a variable by putting the expression in back quotes.

LINE=`egrep ${day} file2.txt`

Of course, if you were looking for the persons name you would have to add to the grep command a pipe to something like cut . You could save that output in the exact same way.
Armed with that variable, lets call it ${PERSON} , you could use it to look for contact information as well by using grep again on the first file.

egrep ${PERSON} file1.txt

I still think you should challenge the need to use either of these two tools as there are better solutions available.

Do i need a for loop or if statment for the name and phone??
I know i need to grep and cut, but how do i set it up like this??
The output file should be like this:
Name----------------On-Call--------------------Phone------------------Start Time
Sally---------------- Monday ---------------267.423.5412-----------------8am
Roberto-------------Tuesday---------------313.432.4532-----------------2am
Alice---------------Wednesday-------------234.576.3245----------------11pm
David---------------Thursday---------------734.854.5643----------------10am
Frank----------------Friday------------------342.465.6754----------------3pm
Mary---------------Saturday-----------------435.432.7654----------------2pm
Ted-----------------Sunday------------------324.642.6743-----------------4pm

Edited 5 Years Ago by bossman5000: n/a

Have you tried either of the two approaches? If so, how did either work out? What went wrong?

I understand what u saying but ....I need another example with a for loop that shows name and phone....how do i structure it like this?
Name----------------On-Call--------------------Phone
Sally---------------- Monday --------------- 267.423.5412

I gave you all the parts you need to do this. What have you tried? What didn't work?

I need help with sorting the two files??
the output was sally monday 8..... i need the phone number??

so far good

Edited 5 Years Ago by bossman5000: n/a

This article has been dead for over six months. Start a new discussion instead.