Operating System - HP-UX
1833792 Members
2032 Online
110063 Solutions
New Discussion

How to group the output of a loop

 
SOLVED
Go to solution
Alvin Ramos
Occasional Contributor

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< 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 < 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
[/CODE]

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. :confused:

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 :D

Thanx in advance :)
10 REPLIES 10
SSCHAER
Advisor
Solution

Re: How to group the output of a loop

----------------------

emp_bk=`sqlplus -s scott/tiger....<set pagesize ....
select e.emp_id||':'||sss_no from employee e, employee_bank eb
where e.emp_id=eb.emp_id;
quit;


output :
4567:45566
2231:59589
1121:55170
2233:51530
....

#
# count lines
if test `echo $emp_bk|wc -l` -lt 7
then echo "one group only"
multigroup=no
else echo "multiple groups"
multigroup=yes
fi
#
# set group number to 1
group_no=1
cnt=1
for x in $emp_bk
do
#
# get employee number, bank number
emp_no=`echo $x|cut -d':' -f1`
bank_no=`echo $x|cut -d':' -f2`

#assemble group name 'GROUP' and actual group number
w_grp="GROUP"$group
#
# execute ss_move
sss_move $bank_no $w_grp

#
# add 1 to counter. if > 2 then add 1 to group counter
# set counter to 1
cnt=$cnt+1
if test $cnt -eq 3
then cnt=1
group_no=$group_no+1
fi
#
# reset group counter to 1 in case we just have one group
if test "$multigroup" = "no"
then group_no=1
fi
done


--------------------------------------------




comment
cnt=$cnt+1 : you need to use expr to increase the number by 1.
James R. Ferguson
Acclaimed Contributor

Re: How to group the output of a loop

Hi:

> cnt=$cnt+1 : you need to use expr to increase the number by 1.

Using 'expr' is slow. Let the shell do the arithmetic:

# cnt=$(($cnt+1))

For example:

# cnt=1;cnt=$(($cnt+1));echo ${cnt}
2

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: How to group the output of a loop

>JRF: Let the shell do the arithmetic: cnt=$(($cnt+1))

Or even better: (( cnt += 1 ))
Alvin Ramos
Occasional Contributor

Re: How to group the output of a loop

@sschaer, thanx - this gives me an idea.

@JRF, thanx

@Dennis Handly,
what does ((cnt += 1)) means (increment then equals to)????

Thanx Guys
James R. Ferguson
Acclaimed Contributor

Re: How to group the output of a loop

Hi:

> cnt=$(($cnt+1))

> (( cnt += 1 ))

These are equivalent operations. The second simply means increment the variable by one and destructively store the new value into the variable. The second form is shorthand for the first form. The first form would be used if you wanted to increment by a value other than one, as for example:

cnt=$(($cnt+2))

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: How to group the output of a loop

>JRF: The first form would be used if you wanted to increment by a value other than one, as for example: cnt=$(($cnt+2))

Perhaps you are confusing the operator ++ with +=. Note the scummy real shell doesn't support ++/--.
To increment by 2: (( cnt += 2 ))
Basically there isn't a need to use the first form if you are doing integer arithmetic.
Dennis Handly
Acclaimed Contributor

Re: How to group the output of a loop

>what does ((cnt += 1)) means (increment then equals to)?

More specifically, in C/C++ a compound assignment is the same as:
L O= R ==> L = L O R
Where O is any of the binary operators.
James R. Ferguson
Acclaimed Contributor

Re: How to group the output of a loop

Hi (again):

> Dennis: Perhaps you are confusing the operator ++ with +=. Note the scummy real shell doesn't support ++/--.

Yes, indeed I was! I know that ++/-- aren't supported by the shell (a damn shame!) but I confess I wasn't aware that one could do '(( cnt += 2 ))'.

Dennis, thanks for the clarification!

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: How to group the output of a loop

>JRF: I confess I wasn't aware that one could do '(( cnt += 2 ))'.

You can also do: (( cnt = aaa + bbb ))
Fredrik.eriksson
Valued Contributor

Re: How to group the output of a loop

To get the output to 1 "group" I would recommend doing it as a function.

This way you can execute the function within a variable and then you have a grouped output.

Something like this might be the key.
#!/usr/bin/sh
function do_dragon {
e_id=$(sqlplus -s scott/tiger@DB< SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF;
select emp_id from employee;
quit
)

group=GROUP1
# Getting the sss_no for each emp_id

for i in $e_id; do
sss_nos=$(sqlplus -s scott/tiger@DB < 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
}

echo "Employee ID's $e_id"

output=$(do_dragon)
echo $output

ps. haven't tested it :P so might need some tweaking ds.

Best regards
Fredrik Eriksson