Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

for loop in awk

42 views
Skip to first unread message

rakeshori

unread,
Apr 8, 2015, 9:51:49 AM4/8/15
to
BELOW is my .csh file

set p_md=0.10
set p_fa=0.10
set p_pu=0.50

foreach n ( 1 2 3 4 5 6 7 8 9 10)
rm n${n}p_md${p_md}p_fa${p_fa}p_pu${p_pu}
end
rm detection

printf "n msg\n ">> detection
foreach n ( 1 2 3 4 5 6 7 8 9 10)
foreach seed ( 1 2 3 4 5 6 7 8 9 10 )
./test ${p_md} ${p_fa} ${p_pu} ${n} ${seed} >> n ${n}p_md${p_md}p_fa${p_fa}p_pu${p_pu}
end

printf "%d\t" $n>>"detection"
awk 'BEGIN{count=0;sum0=0} {sum0 +=$1; count++} END {avg0=sum0/count; printf "%0.2f\t\n",avg0 }' n${n}p_md${p_md}p_fa${p_fa}p_pu${p_pu}>> detection
end

and my result in detection file is

n msg
1 5.00
2 18.00
3 47.00
4 125.00
5 300.00
6 773.00
7 1837.00
8 4718.00
9 11205.00
10 28783.00

how do i change in my .csh file such that i can get the summation of the values of msg again.

Thank U

Janis Papanagnou

unread,
Apr 8, 2015, 11:02:31 AM4/8/15
to
On 08.04.2015 15:51, rakeshori wrote:
> BELOW is my .csh file
>
> set p_md=0.10
> set p_fa=0.10
> set p_pu=0.50
>
> foreach n ( 1 2 3 4 5 6 7 8 9 10)
> rm n${n}p_md${p_md}p_fa${p_fa}p_pu${p_pu}
> end
> rm detection
>
> printf "n msg\n ">> detection
> foreach n ( 1 2 3 4 5 6 7 8 9 10)
> foreach seed ( 1 2 3 4 5 6 7 8 9 10 )
> ./test ${p_md} ${p_fa} ${p_pu} ${n} ${seed} >> n ${n}p_md${p_md}p_fa${p_fa}p_pu${p_pu}
> end
>
> printf "%d\t" $n>>"detection"
> awk 'BEGIN{count=0;sum0=0} {sum0 +=$1; count++} END {avg0=sum0/count; printf "%0.2f\t\n",avg0 }' n${n}p_md${p_md}p_fa${p_fa}p_pu${p_pu}>> detection

You don't need these two initializations in awk, they are the default.

awk '{sum0+=$1; count++} END {avg0=sum0/count; printf "%0.2f\t\n",avg0}'

> end
>
> and my result in detection file is
>
> n msg
> 1 5.00
> 2 18.00
> 3 47.00
> 4 125.00
> 5 300.00
> 6 773.00
> 7 1837.00
> 8 4718.00
> 9 11205.00
> 10 28783.00
>
> how do i change in my .csh file such that i can get the summation of the values of msg again.

Asked that way I'd say: revert your changes. But...

This is off-topic in this awk newsgroup. While comp.unix.shell would certainly
the better address you will probably be pointed out there that you should not
use csh for programming but switch to any bourne like shell for the well known
reasons.

Janis

>
> Thank U
>

0 new messages