Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2009
    Posts
    8

    Question Unanswered: How to group the output of a loop

    Hi Guys,

    First time user & new to scripting.

    I have my shell script like this:

    Code:
    #!/usr/bin/sh
    e_id=`sqlplus -s scott/tiger@DB<<eof
                SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF;
                select emp_id from employee;
                quit
                `
    echo "Employee ID's $e_id"
    
    group=GROUP1
    # Getting the sss_no for each emp_id
    
    for i in $e_id 
     do
       sss_nos=`sqlplus -s scott/tiger@DB <<eof
              SET PAGES 0 LINES 500 HEAD OFF;
              select sss_no from employee_bank where emp_id = $i;
              quit
              `
       echo "List of SSS no's $sss_nos"
       # Run a customize program that calls the sss_nos
       # SSS_move
       SSS_move $sss_nos $group
      sleep 2
    done
    The Output:
    Employee ID's
    4567
    2231
    1121
    2233
    4554
    3243
    1231
    3311

    List of SSS no's
    45566
    59589
    55170
    51530
    33099
    20234
    87231
    54192

    SSS_move:
    SSS_move 45566 GROUP1
    SSS_move 59589 GROUP1
    SSS_move 55170 GROUP1
    SSS_move 51530 GROUP1
    SSS_move 33099 GROUP1
    SSS_move 20234 GROUP1
    SSS_move 87231 GROUP1
    SSS_move 54192 GROUP1

    Now, how will I able to group the output of the loop so that I can assign GROUP1 only to the 1st two output of the loop then assign GROUP2 to the 2nd two output of the loop & so on.

    Desired Output:
    SSS_move 45566 GROUP1
    SSS_move 59589 GROUP1
    SSS_move 55170 GROUP2
    SSS_move 51530 GROUP2
    SSS_move 33099 GROUP3
    SSS_move 20234 GROUP3
    SSS_move 87231 GROUP4
    SSS_move 54192 GROUP4

    Also, how do I count the $sss_nos output so that I have this condition:
    If my $sss_nos -gt 7 (w/c in my example above it is true) then assign to multiple GROUP#. If it is -lt 7 then group it only to GROUP1

    Pls. help

    Thanx in advance

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    I think this is easier to solve using SQL, not shell commands. However, if you insist, you can count lines in anything with "wc -l", and awk seems to me a suitable tool for assigning different GROUPs.
    ---
    "It does not work" is not a valid problem statement.

  3. #3
    Join Date
    Jul 2009
    Posts
    8

    Talking

    Can you help me by posting your SQL solution. I love to know it.

    Thanx

  4. #4
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool SQL solution

    One way would be something like this:
    Code:
    #!/usr/bin/ksh
    # Getting the sss_no for each emp_id
    sqlplus -s scott/tiger@ORCL <<EOF >sss_nos
    SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
    SELECT 'SSS_Move ' || sss_no || ' GROUP' || ROUND ((ROWNUM) / 2, 0)
          ||CHR(10)||'sleep 2'
      FROM emp, emp_bank
     WHERE emp_bank.employee_id = emp.employee_id
    /
    quit
    EOF
    echo "List of SSS no's:"
    cat sss_nos
    # Run the customize program that calls the sss_nos
    # SSS_move:
    . sss_nos
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  5. #5
    Join Date
    Jul 2009
    Posts
    8
    Cool

    Thanx

  6. #6
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Talking No Problemo

    Quote Originally Posted by alvingo
    Cool

    Thanx
    No Problemo, any time...
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Posting Permissions

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