Question in loops - Unix

This is a discussion on Question in loops - Unix ; 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 ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Question in loops

  1. Question in loops

    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. Re: Question in loops

    To be honest I would start by sorting both files by name and then looping through the files and picking out each column (names, phone numbers, etc). This would would make it extremely easy to loop through.

    Just an example:

    file1="$(cat file1|sort)"

    ####
    $ cat file1|sort
    Alice 234.576.3245
    David 734.854.5643
    Frank 342.465.6754
    Mary 435.432.7654
    Roberto 313.432.4532
    Sally 267.423.5412
    Ted 324.642.6743
    ####

    Then maybe something like:

    lineCount=0
    for line in $(echo "$file1"|awk '{print $1}')
    do
    names[$lineCount]=$line
    let lineCount++
    done

    which the above gives you an array:
    [Alice David Frank Mary Roberto Sally Ted]

    Let me know what you think.

  3. Re: Question in loops

    I am using bash shell in Linux. I do not know how to find out which version it is.
    When I run a loop like this:

    set x=5
    if [ $x -le 10] then
    echo "TRUE"
    else
    echo "FALSE \a"

    I get an error "[ : -lt : unary operator expected".
    Can someone please help me with this?
    Any help appreciated.
    I have an exam the following week so ASAP.
    Thanks in advance.
    adult merchant account

+ Reply to Thread