If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > How to group the output of a loop

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-15-09, 02:55
alvingo alvingo is offline
Registered User
 
Join Date: Jul 2009
Posts: 8
Question 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
Reply With Quote
  #2 (permalink)  
Old 07-15-09, 08:22
n_i n_i is online now
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,230
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.
Reply With Quote
  #3 (permalink)  
Old 07-15-09, 22:10
alvingo alvingo is offline
Registered User
 
Join Date: Jul 2009
Posts: 8
Talking

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

Thanx
Reply With Quote
  #4 (permalink)  
Old 07-16-09, 18:22
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,413
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
Reply With Quote
  #5 (permalink)  
Old 07-20-09, 07:50
alvingo alvingo is offline
Registered User
 
Join Date: Jul 2009
Posts: 8
Cool

Thanx
Reply With Quote
  #6 (permalink)  
Old 07-21-09, 11:01
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,413
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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On