Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2011
    Posts
    6

    Unanswered: looping problem in UNIX script

    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

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    So the output of line "grep $day file2" is
    "Sally Monday 8"
    If you read that line as Name Day start,
    you can grep $name in file1 and get the telephone number which you can then print.
    However, this technique would not work for a large file, as both file1 and file2 are completely read 7 times, and sure enough as you get bigger there will be two people working on the same day, and there will be Frank A. and Frank M., or Ann and Anna.

    The problem that you have stated by the way is the core issue that this forum tries to solve.
    Consider setting up 7 directories (Monday thru Sunday) and removing the day from file2 and having a file named Sally containing the value 8 in the Monday directory.
    so now your have:
    Monday/Sally
    Tuesday/Roberto
    etc
    Now you can change "grep Monday file2"
    to
    "ls Monday"
    and the output is only those scheduled for Monday. You can then "cat Monday/Sally" to get the start time.
    Going a step further, create directories for each person (eliminating file1) and each person's directory contains a file named phone that has the phone number as its contents.

    As far as the time goes, use an if..else construct.
    if $time > 12
    then
    print $time - 12 'pm'
    else
    print $time 'am'
    fi
    You will have to add additional code if you have shifts starting at noon and midnight.
    Last edited by kitaman; 10-22-11 at 10:45.

  3. #3
    Join Date
    Oct 2011
    Posts
    6
    I didn't understand what you mean do i have to use 2 loops while and for, or nested loops?

    Thanks

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Something like this:
    Code:
    for day in Monday Tuesday Wednesday Thursday Friday Saturday Sunday
    do
    grep $day file2 >temp.file
    cat temp.file|read name day1 start
    grep $name file1 >temp1.file
    cat temp1.file|read name2 phone
    echo $name $day1 $phone $start
    done
    I'll leave you to figure out how to do it without temp.file and temp1.file

  5. #5
    Join Date
    Oct 2011
    Posts
    6
    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

  6. #6
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Spaces are important in expr. There must be a space before and after each operator.
    Start=`expr $start - 12`
    not
    Start=`expr $start-12`
    or Start=`expr $start -12`

    Will you post your solution, after you have submitted it?
    Last edited by kitaman; 10-25-11 at 10:11.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •