按间隔分组数字:我有一个介于0.0-3.6之间的数字数组

时间:2014-01-02 16:11:27

标签: linux bash fortran

我有一个介于0.0-3.6之间的数字数组,我想知道这些数字中有多少是在这个区间:0.0-0.2,0.2-0.4等等,有人可以帮助我吗?非常感谢... 我在fortran中试过这个:

IMPLICIT NONE

INTEGER :: acoun, bcoun, ccoun, dcoun, ecoun, fcoun, gcoun, hcoun, icoun 
INTEGER ::  jcoun,  kcoun,  lcoun,  mcoun,  ncoun,  ocoun,  pcoun,  qcoun, ia
INTEGER :: i1(4), i2(30), i3(30), i4(30), i5(30), i6(30), i7(30), i8(30), i9(30), i10(30), &
i11(30), i12(30), i13(30), i14(30), i15(30), i16(30), i17(30)
REAL    :: a

OPEN(UNIT=10,FILE='DAN1',STATUS='OLD')

acoun=0.0
bcoun=0.0
ccoun=0.0
dcoun=0.0
ecoun=0.0
fcoun=0.0
gcoun=0.0
hcoun=0.0
icoun=0.0
jcoun=0.0
kcoun=0.0
lcoun=0.0
mcoun=0.0
ncoun=0.0
ocoun=0.0
pcoun=0.0
do i=1,119
READ(10,*)a
 if (( a < 0.2d0 ).and.(a > 0.0d0) )then
    i1(acoun)=i
    acoun=acoun+1
 elseif (( a < 0.4d0 ).and.(a > 0.2d0) )then
    i2(bcoun)=i
    bcoun=bcoun+1
 elseif (( a < 0.6d0 ).and.(a > 0.4d0) )then
    i3(ccoun)=i
    ccoun=ccoun+1
 elseif (( a < 0.8d0 ).and.(a > 0.6d0) )then
    i4(dcoun)=i
    dcoun=dcoun+1
 elseif (( a < 1.0d0 ).and.(a > 0.8d0) )then
    i5(ecoun)=i
    ecoun=ecoun+1
 elseif (( a < 1.2d0 ).and.(a > 1.0d0) )then
    i6(fcoun)=i
    fcoun=fcoun+1
 elseif (( a < 1.4d0 ).and.(a > 1.2d0) )then
    i7(gcoun)=i
    gcoun=gcoun+1
 elseif (( a < 1.6d0 ).and.(a > 1.4d0) )then
    i8(hcoun)=i
    hcoun=hcoun+1
 elseif (( a < 1.8d0 ).and.(a > 1.6d0) )then
    i9(icoun)=i
    icoun=icoun+1     elseif (( a < 2.0d0 ).and.(a > 1.8d0) )then
    i10(jcoun)=i
    jcoun=jcoun+1
 elseif (( a < 2.2d0 ).and.(a > 2.0d0) )then
    i11(kcoun)=i
    kcoun=kcoun+1
 elseif (( a < 2.4d0 ).and.(a > 2.2d0) )then
    i12(lcoun)=i
    lcoun=lcoun+1
 elseif (( a < 2.6d0 ).and.(a > 2.4d0) )then
    i13(mcoun)=i
    mcoun=mcoun+1
 elseif (( a < 2.8d0 ).and.(a > 2.6d0) )then
    i14(ncoun)=i
    ncoun=ncoun+1
 elseif (( a < 3.0d0 ).and.(a > 2.8d0) )then
    i15(ocoun)=i
    ocoun=ocoun+1
 elseif (( a < 3.2d0 ).and.(a > 3.0d0) )then
    i16(pcoun)=i
    pcoun=pcoun+1
 elseif (a > 3.2d0)then
    i17(qcoun)=i
    qcoun=qcoun+1
 endif
enddo
     WRITE(*,*)  i1(acoun),acoun
 WRITE(*,*)  i2(bcoun),bcoun
 WRITE(*,*)  i3(ccoun),ccoun
 WRITE(*,*)  i4(dcoun),dcoun
 WRITE(*,*)  i5(ecoun),ecoun
 WRITE(*,*)  i6(fcoun),fcoun
 WRITE(*,*)  i7(gcoun),gcoun
 WRITE(*,*)  i8(hcoun),hcoun
 WRITE(*,*)  i9(icoun),icoun
 WRITE(*,*) i10(jcoun),jcoun
 WRITE(*,*) i11(kcoun),kcoun
 WRITE(*,*) i12(lcoun),lcoun
 WRITE(*,*) i13(mcoun),mcoun
 WRITE(*,*) i14(ncoun),ncoun
 WRITE(*,*) i15(ocoun),ocoun
 WRITE(*,*) i16(pcoun),pcoun
 WRITE(*,*) i17(qcoun),qcoun

close(10)

ENDPROGRAM sorting"

此文件提供了一些值,但它们不正确此程序在每个时间间隔内超量计算。

我在bash中试过这个

我使用if语句尝试使用bash:

if [$var > 0.0 -a 0.2 < $var]then 
echo "$var" >> file
wc -l file

但它也不起作用

感谢很多帮助......

1 个答案:

答案 0 :(得分:0)

Bash不支持浮点运算,但是awk不支持:

假设file包含所有数字,在不同的行中,此脚本应该执行您想要的操作:

$ awk '{a[int($1/0.2)]++}END{for(i in a)print i*0.2 "-" (i+1)*0.2, a[i]}' file | sort -n